如果我有兩個表... Category和Pet。LINQ to SQL Table Dependency
有沒有辦法讓LINQ to SQL將聯合查詢映射的結果映射到另一個強類型類(如:PetWithCategoryName),以便我可以強制將它傳遞給MVC視圖?
我目前有類別和寵物類...我應該再做一個嗎?
也許我在這裏錯過了一些東西。你們能開導我嗎?
from p in petTable
join c in categoryTable on p.CategoryId equals c.Id
where (c.Id == categoryId.Value)
select new
{
p.Id,
p.Name,
p.Description,
p.Price,
CategoryName = c.Name
}
<?xml version="1.0" encoding="utf-8" ?>
<Database Name="PetShop" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
<Table Name="Category" Member="PetShop.Models.Category">
<Type Name="PetShop.Models.Category">
<Column Name="Id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" />
<Column Name="Name" Member="Name" />
<Column Name="Description" Member="Description" />
</Type>
</Table>
<Table Name="Pet" Member="PetShop.Models.Pet">
<Type Name="PetShop.Models.Pet">
<Column Name="Id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" />
<Column Name="Name" Member="Name" />
<Column Name="Description" Member="Description" />
<Column Name="ImageUrl" Member="ImageUrl" />
<Column Name="Price" Member="Price" />
<Column Name="CategoryId" Member="CategoryId" />
<Association Name="FK_Pet_Category" Member="Category" ThisKey="CategoryId" OtherKey="Id" IsForeignKey="true" />
</Type>
</Table>
</Database>
啊,我喜歡LoadWith選項......我得去找那個。我很想加載MyPet.Category.Name。謝謝 – 2008-10-08 11:46:39
我會如何使用LoadWith?我在網上找不到太多幫助。任何好的資源? – 2008-10-08 11:51:15