有条件的包括在linq到实体? [英] conditional include in linq to entities?

查看:155
本文介绍了有条件的包括在linq到实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想做的是使用include方法塑造我的结果,即定义沿着对象图形遍历的距离。但是...我希望遍历是有条件的。

 像... 

经销商
.include(d => d.parts.where(p => p.price< 100.00))
.include(d => d.parts.suppliers.where s => s.country ==brazil));

我知道这不是有效的linq,其实这是可怕的错误,但本质上我是'寻找一些方法来构建一个表达式树,它将返回形状的结果,相当于...

  select * $ b来自经销商的$ b作为d 
外部连接部件作为p on d.dealerid = p.dealerid
和p.price< 100.00
外部加入供应商p.partid = s.partid
和s.country ='brazil'

,重点是加入条件。



我觉得这与esql相当直截了当,但是我的偏好是快速构建表达式树。

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

解决方案

使用(TestEntities db = new TestEntities())
{
var query = from d in

  db.Dealership 
选择新
{
经销商= d,
零件= d.Part.Where

p => p.Price< 100.0
&& p.Supplier.Country ==Brazil
),
Suppliers = d.Part.Select(p => p.Supplier)
} ;

var dealers = query.ToArray()。选择(o => o.Dealer);
foreach(经销商的经销商)
{
Console.WriteLine(dealer.Name);
foreach(var part in dealer.Part)
{
Console.WriteLine(+ part.PartId +,+ part.Price);
Console.WriteLine


+ part.Supplier.Name
+,
+ part.Supplier.Country
);
}
}
}

这段代码会给你一个经销商列表每个都包含已过滤的零件列表。每个部分引用供应商。有趣的是,您必须以所显示的方式在select中创建匿名类型。否则,经销商对象的零件属性将为空。



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

  var dealers = query.ToArray() o = D); 

但我同意Darren,这可能不是您的图书馆用户期待的。 p>

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"));

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'

with an emphasis on the join conditions.

I feel like this would be fairly straight forward with esql but my preference would be to build expression trees on the fly.

as always, grateful for any advice or guidance

解决方案

This should do the trick:

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
                );
        }
    }
}

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.

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);

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

这篇关于有条件的包括在linq到实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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