[英]ENTITY FRAMEWORK OrderBy on Navigation Property (one-to-many)
In my EF CodeFirst DB, I have two entities: 在我的EF CodeFirst DB中,我有两个实体:
public class Prod
{
public int ProductId {get;set;}
......
......
public ICollection<ProdLang> ProdLangs {get;set;}
}
public class ProdLang
{
public int ProdLangId {get;set;}
public int LangId {get;set;}
public string Name {get;set;}
public string Description {get;set;}
......
......
public Product Product {get;set;}
}
With Eager Loading, I want to load my collection of Prod ordered by ProdLang.Name or ProdLang.Description for example, by a single Langid 使用“急切加载”,我想加载由ProdLang.Name或ProdLang.Description排序的Prod集合,例如,由单个Langid排序
What can I do? 我能做什么?
Thanks. 谢谢。
Do you mean load your collection of ProdLang ordered by ... (as opposed to collection of Prod)? 您的意思是加载由...排序的ProdLang集合(而不是Prod的集合)吗? If so, this should work
如果是这样,这应该可行
public class Prod
{
private ICollection<ProdLang> _prodLangs;
public ICollection<ProdLang> ProdLangs
{
get{ return _prodLangs.OrderBy(p => p.Name).ToList(); }
set{ _prodLangs = value; }
}
}
I can't see ProductId property in your ProdLang class to join with Prod class, but I think that it exists. 我看不到您的ProdLang类中的ProductId属性与Prod类一起加入,但是我认为它存在。
So, you need to do : 因此,您需要执行以下操作:
var products = from p in Context.Prod.Set
from pl in Context.ProdLang.Set
where (p.ProductId == pl.ProductId)
orderby (pl.Name)
select p;
If you want to get ProdLang details, you can use a dynamic type: 如果要获取ProdLang详细信息,可以使用动态类型:
var products = from p in Context.Prod.Set
from pl in Context.ProdLang.Set
where (p.ProductId == pl.ProductId)
orderby (pl.Name)
select new { Prod = p, ProdLang = pl };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.