凡收集条款条款

2023-09-02 21:12:39 作者:泛滥的思念

我使用的是从朱莉勒曼的的DbContext书BAGA code。我想重新在LINQ以下SQL查询,并把结果列表集合时遇到的问题。 http://learnentityframework.com/downloads/

I'm using the BAGA code from Julie Lerman's DbContext book. I want to recreate the following sql query in linq and put the results in a List collections and am having problems. http://learnentityframework.com/downloads/

SELECT * FROM baga.Locations d
LEFT JOIN Lodgings l ON d.LocationID = l.destination_id
WHERE d.Country = 'usa'
AND (l.MilesFromNearestAirport > 5 or l.MilesFromNearestAirport is null)

所以,在英语,让那些在美国和包括所有相关的住宿的所有位置(目的地),其中MilesFromNearestAirport> 5

So, in english, get all locations (destinations) that are in the USA and include all the related lodgings where MilesFromNearestAirport > 5

语法不能编译,但我希望下面

The syntax doesn't compile but I was hoping for something similar to below

var dests = context.Destinations
  .Where(d => d.Country == "USA" && d.Lodgings.Where(l => l.MilesFromNearestAirport > 5))
  .Select(d => d)
  .ToList();

任何想法?

推荐答案

由于@Sampath说,这通常与导航性能做,但我相信,他的code不正是你想要的。这(我认为)接近:

As @Sampath says, this is normally done with navigation properties but I believe that his code does not exactly do what you want. This (I think) is closer:

var dests = context.Destinations
           .Where(d => d.Country == "USA")
           .Select(d => 
               new { d,
                     RemoteLodgings = d.Lodgings
                         .Where(l => l.MilesFromNearestAirport > 5)}
           .ToList();

但它仍然不会做你希望,如果我把你的要求,信什么。你想要的位置与住宿的包括的,即导航属性加载的部分。这可以方便的时候实体要序列和在一个封装发送到(网络)客户端。 (尽管上述方法将是该行太)。

But it still does not do what you want if I take your requirement to the letter. You want the location with the lodgings included, i.e. the navigation properties loaded partly. This can be convenient when the entities are to be serialized and sent to a (web) client in one package. (Although the method above would be OK for that too).

但有可能使集合与部分装导航属性。

But is is possible to make a collection with partly loaded navigation properties.

这本书,在第40页,显示了如何一个单一的实体部分负荷性能的导航(简称: context.Entry(实体).Collection(E => e.Children)。查询()。如果(条件)。但正如表示,这只是一个实例。它不这样做,对实体的集合的最好方法。

The book, at page 40, shows how you can partly load navigation properties of a single entity (in short: context.Entry(entity).Collection(e => e.Children).Query().Where(condition). But as said, that's only one instance. It's not the best method to do that for a collection of entities.

有趣的是(因为我的一个同事发现了),你可以很容易地通过单独加载所需要的集合元素融入背景下做到这一点对实体的集合:

The funny thing is (as a colleague of mine found out), you can easily do it for a collection of entities by loading the required collection elements into the context separately:

var lodgings = context.Lodgings
              .Where(l => l.MilesFromNearestAirport > 5 
                       && l.Destination.Country == "USA")
              .ToList();

现在,如果通过你循环context.Destinations.Where(D => d.Country ==USA),你会看到,他们的住处都装有一> 5 。可能是因为在这一点上EF执行的关系修正。 (延迟加载禁用,因为延迟加载会完全加载导航性能)。

Now if you loop through context.Destinations.Where(d => d.Country == "USA") you will see that their lodgings are loaded with the ones ">5". Probably because at this point EF executed relationship fixup. (Lazy loading disabled, because lazy loading will fully load the navigation properties).

修改(您的评论之后) 当你说这是一个黑客位,我不能同意。其实我忘了提及,摆在首位。问题是,整个机制崩溃时延迟加载发生的人谁不知道的了code是什么激活。我不喜欢code依赖于国家的方式,并不明显。所以,我总是preFER第一种方式。

Edit (after your comment) I couldn't agree more when you say it is a bit of a hack. Actually I forgot to mention that in the first place. The problem is that the whole mechanism collapses when lazy loading happens to be activated by someone who's not aware of the what the code is for. I don't like code that depends on state in a way that is not obvious. So I would always prefer the first approach.