如何使用 linq to sql 和 ef 查询导航属性
我正在尝试使用 linq to sql强类型查询 3 个 ef 对象。与产品和类别存在一对多关系。我的类包含导航属性,如下所示。
public partial class Product
{
public int ID {get;set;}
public string Name {get;set;}
public virtual ICollection<Group> NpGroup {get;set;}
}
public partial class Category
{
public int ID {get;set;}
public string Name {get;set;}
public virtual ICollection<Group> NpGroup {get;set;}
}
public partial class Group
{
public int ID {get;set;}
public int ProductID {get;set;}
public int CategoryID {get;set;}
public virtual Product NpProduct {get;set;}
public virtual Category NpCategory {get;set;}
}
尝试避免基于字符串的 .Include(),我将如何构造一个返回等于 ProductID“1”但还包含产品和类别名称的组的查询?
比如:
var context = ObjectContext.CurrentObjectContext;
var query = from c in context.Group
where c.ProductID == 1
//Include the names of the product and category of the group record (c.NpProduct.Name etc.)
select c;
我可能错过了森林中的树木,但我似乎无法获得 ObjectContext.LoadProperty 的语法(如果这是正确的方法)。
有什么想法吗?谢谢。
I am trying to strongly type a query for 3 ef objects using linq to sql. There are one-to-many relationships with product and category. My classes contain navigation properties and look like this.
public partial class Product
{
public int ID {get;set;}
public string Name {get;set;}
public virtual ICollection<Group> NpGroup {get;set;}
}
public partial class Category
{
public int ID {get;set;}
public string Name {get;set;}
public virtual ICollection<Group> NpGroup {get;set;}
}
public partial class Group
{
public int ID {get;set;}
public int ProductID {get;set;}
public int CategoryID {get;set;}
public virtual Product NpProduct {get;set;}
public virtual Category NpCategory {get;set;}
}
Trying to avoid the string based .Include(), how would I construct a query that returned a group equal to ProductID "1" but also included the names of the product and category?
Something like:
var context = ObjectContext.CurrentObjectContext;
var query = from c in context.Group
where c.ProductID == 1
//Include the names of the product and category of the group record (c.NpProduct.Name etc.)
select c;
I am probably missing the trees through the forest but I can not seem to get the syntax of ObjectContext.LoadProperty (if that is the right way to go).
Any thoughts? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我怀疑您同时使用 L2SQL 和 EF,所以尽量不要让人们感到困惑。
无论如何,对于 EF - 有两种方法来加载导航属性:
1 - 预加载
2 - 显式加载
差异是选项 2) 导致 2 个查询,选项 1 导致 FK 上的内部联接。
我可以同情您因为“神奇字符串”而不愿意使用 Include - 我不确定为什么 EF 团队没有将它们设为强类型,但我确信这是有充分理由的。
有关加载/包含的更多信息请参见此处。
华泰
First of all, i doubt you are using both L2SQL and EF, so try not to confuse people.
Anyways, with EF - there is two ways to load the navigational properties:
1 - Eager Loading
2 - Explicit Load
Difference is option 2) causes 2 queries, option 1 causes an inner join on the FK.
I can sympathise with your reluctance to use Include because of the 'magic strings' - i'm not sure why the EF team didn't make them strongly typed, however im sure there was a good reason.
More info on Load/Include here.
HTH
我认为我们都讨厌在 .include() 语句中使用类型化字符串。
我开始使用枚举来表示表名,只是为了避免拼写错误等。
对于包含大约 70 个表的数据库,我花了 10 分钟。创建枚举,我的 linq 现在是这样的:
var context = ObjectContext.CurrentObjectContext;
var query = from c in context.Group.Include(TableEnum.Category.ToString())
其中 c.ProductID == 1
选择c;
再说一次,并不完美,但至少它经过了编译器的检查
I think we all hate to use the typed string in the .include() statement.
I've started to use a enum to represent the table name, just to avoid spelling errors, etc.
for my database of about 70 tables it took me 10 min. to create the enum and my linq is now something like this:
var context = ObjectContext.CurrentObjectContext;
var query = from c in context.Group.Include(TableEnum.Category.ToString())
where c.ProductID == 1
select c;
Again, not perfect, but at least it's checked by the compiler