本文介绍了有条件地包含在 linq to entity 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得以下应该是可能的,但我不确定要采取什么方法.

I felt like the following should be possible I'm just not sure what approach to take.

我想要做的是使用包含方法来塑造我的结果,即定义沿对象图遍历多远.但是...我希望这种遍历是有条件的.

What I'd like to do is use the include method to shape my results, ie define how far along the object graph to traverse. but... I'd like that traversal to be conditional.

something like...

dealerships
    .include( d => d.parts.where(p => p.price < 100.00))
    .include( d => d.parts.suppliers.where(s => s.country == "brazil"));

我知道这不是有效的 linq,事实上,这是非常错误的,但本质上我正在寻找某种方法来构建一个表达式树,该树将返回成形的结果,相当于......

I understand that this is not valid linq, in fact, that it is horribly wrong, but essentially I'm looking for some way to build an expression tree that will return shaped results, equivalent to...

select *
from dealerships as d
outer join parts as p on d.dealerid = p.dealerid
    and p.price < 100.00
outer join suppliers as s on p.partid = s.partid
    and s.country = 'brazil'

强调连接条件.

我更喜欢动态构建表达式树.

my preference would be to build expression trees on the fly.

一如既往,感谢您的任何建议或指导

as always, grateful for any advice or guidance

推荐答案

这应该可以解决问题:

using (TestEntities db = new TestEntities())
{
    var query = from d in db.Dealership
                select new
                {
                    Dealer = d,
                    Parts = d.Part.Where
                    (
                        p => p.Price < 100.0
                             && p.Supplier.Country == "Brazil"
                    ),
                    Suppliers = d.Part.Select(p => p.Supplier)
                };

    var dealers = query.ToArray().Select(o => o.Dealer);
    foreach (var dealer in dealers)
    {
        Console.WriteLine(dealer.Name);
        foreach (var part in dealer.Part)
        {
            Console.WriteLine("  " + part.PartId + ", " + part.Price);
            Console.WriteLine
                (
                "  "
                + part.Supplier.Name
                + ", "
                + part.Supplier.Country
                );
        }
    }
}

此代码将为您提供一个经销商列表,每个经销商列表都包含经过筛选的零件列表.每个部分都引用一个供应商.有趣的部分是您必须以显示的方式在选择中创建匿名类型.否则 Dealership 对象的 Part 属性将为空.

This code will give you a list of Dealerships each containing a filtered list of parts. Each part references a Supplier. The interesting part is that you have to create the anonymous types in the select in the way shown. Otherwise the Part property of the Dealership objects will be empty.

此外,您必须在从查询中选择经销商之前执行 SQL 语句.否则经销商的零件属性将再次为空.这就是我将 ToArray() 调用放在以下行中的原因:

Also, you have to execute the SQL statement before selecting the dealers from the query. Otherwise the Part property of the dealers will again be empty. That is why I put the ToArray() call in the following line:

var dealers = query.ToArray().Select(o => o.Dealer);

但我同意 Darren 的观点,即这可能不是您图书馆的用户所期望的.

But I agree with Darren that this may not be what the users of your library are expecting.

这篇关于有条件地包含在 linq to entity 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:06