跨多个关系级别的实体框架查询 [英] Entity Framework query across multiple levels of relationship

查看:50
本文介绍了跨多个关系级别的实体框架查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才刚开始使用Entity Framework和Linq To Entities,并试图使我的头转向查询.

I'm just beginning in Entity Framework and Linq To Entities and am trying to get my head around querying.

我的数据结构如下:

表A,B,C

A与B有一对多的关系,B与C有一对多的关系.

A has one to many relationship to B, B has one to many relationship to C.

我们的一个演示对象由A,B和A的数据组成. C给出了C的ID

One of our presentation objects is composed of data from A, B & C given the Id from C

那么,如何在查询中表示呢?

So, how can I represent this in a query?

如何从where c.Id == myParam查询中获得A实体?

How do I get an A entity from a where c.Id == myParam query?

推荐答案

有关:

var c = context.Cs.Include("B.A").Where(c => c.Id == myParam).SingleOrDefault();

BCB实例的导航属性,而ABA实例的导航属性.

Where B is navigation property on C to instance of B an A is navigation property from B to instance of A.

如果System.Data.Entity命名空间得到重新引用,您也可以使用lambda表示法:

You can also use lambda notation if System.Data.Entity namespace is refeneced:

var c = context.Cs.Include(i=>i.B.A).Where(c => c.Id == myParam).SingleOrDefault();

对于Collection导航属性,您可以使用.Select()

And for Collection navigation properties you can use .Select()

var c = context.Cs.Include(i=>i.Select(j=>j.A)).Where(c => c.Id == myParam).SingleOrDefault();

这篇关于跨多个关系级别的实体框架查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆