I am currently developing my first database driven, .NET MVC application and in it I have a table called Animals that contains a number of fields. One field is the breed of the animal, in this field I am holding an Id to another Breed table where the text for the breed is actually stored.
我目前正在开发我的第一个数据库驱动的.NET MVC应用程序,在其中我有一个名为Animals的表,其中包含许多字段。一个领域是动物的品种,在这个领域我持有一个Id到另一个Breed表,其中实际存储了该品种的文本。
Using LINQ, how to I join the tables and then send the data to the view so it can be viewed in a tabled format like so :
使用LINQ,如何连接表,然后将数据发送到视图,以便以表格格式查看,如下所示:
In the image where the Breed column is displaying a list of numbers, I want it to be the actual bred related to that in the Breed table
在Breed列显示数字列表的图像中,我希望它是与Breed表中相关的实际bred
1 个解决方案
#1
0
use Navigation Properties, which you can generate from your model.
使用导航属性,您可以从模型生成。
Then your class should look like that.
然后你的班级应该是那样的。
public class Animals {
//primitive properties
public int Id {get;set;}
//etc.
//Navigation Properties
public virtual Breeds Breeds {get;set;}
}
public class Breeds {
//prmitive properties
public int Id {get;set;}
public string Name {get;set;}
//Navigation properties
public virtual ICollection<Animals> Animals {get;set;}
}
then to get an entity with a navigation property :
然后获取具有导航属性的实体:
var animal = Repository.Animals.Include(m => m.Breed)
.FirstOrDefault(m => m.Id == <someId>);
and you can use (imagine the model of your view is an Animals
)
并且您可以使用(想象您的视图模型是动物)
@Html.Displayfor(m => m.Breed.Name)
#1
0
use Navigation Properties, which you can generate from your model.
使用导航属性,您可以从模型生成。
Then your class should look like that.
然后你的班级应该是那样的。
public class Animals {
//primitive properties
public int Id {get;set;}
//etc.
//Navigation Properties
public virtual Breeds Breeds {get;set;}
}
public class Breeds {
//prmitive properties
public int Id {get;set;}
public string Name {get;set;}
//Navigation properties
public virtual ICollection<Animals> Animals {get;set;}
}
then to get an entity with a navigation property :
然后获取具有导航属性的实体:
var animal = Repository.Animals.Include(m => m.Breed)
.FirstOrDefault(m => m.Id == <someId>);
and you can use (imagine the model of your view is an Animals
)
并且您可以使用(想象您的视图模型是动物)
@Html.Displayfor(m => m.Breed.Name)