结合LINQ查询? [英] Combine LINQ queries?

查看:70
本文介绍了结合LINQ查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是C#和LINQ的新手,发现它是一个陡峭的山坡!我不知道如何构建这个特定的LINQ查询并将其分解为两个独立的部分以使其工作。

我敢肯定我应该能够将它们组合起来但是它的目前我不知道。



无论如何,这个问题。我有WmsUser类,它包含一个WMSUserGroup类,并且挂起来的是WmsUserGroupDepotCollection,其中每个成员都包含一个权限集合。目前我正在检索两个命中的权限类,首先是使用我的boundObject中包含的DepotId从WmsUserGroupDepotCollection中获取WmsUserGroupDepot。然后从WmsUserGroupDepot中检索WmsUserGroupDepotPermission。任何人都可以通过展示如何结合这两个陈述来帮助我吗?



提前谢谢

Dave

< pre lang =c#> WmsUserGroupDepot ugDepot =(
来自 o in ((App) App.Current).WmsUser.WmsUserGroup.WmsUserGroupDepotCollection
其中 o.DepotId == boundObject.DepotId
选择 o).SingleOrDefault();

WmsUserGroupDepotPermission depotPermission =
来自 o ugDepot.WmsUserGroupDepotPermissionCollection
其中 o.SectionId ==( long )PermissionsSectionEnum.Setup
选择 o).SingleOrDefault();

解决方案

是的,你可以把它变成一个查询,甚至一个班轮。我不确定你是否需要,但你可以用这些方式做到这一点。



  var  depotPermission = WmsUser.WmsUserGroup.WmsUserGroupDepotCollection 
.SingleOrDefault(x => x.DepotId == boundObject.DepotId)
.WmsUserGroupDepotPermissionCollection
.SingleOrDefault(p => p.SectionId ==( long )PermissionsSectionEnum.Setup);









  var  depotPermission =  o 中的  WmsUser.WmsUserGroup.WmsUserGroupDepotCollection 
其中​​ o.DepotId == boundObject.DepotId
选择 o.WmsUserGroupDepotPermissionCollection
.S ingleOrDefault(x => x.SectionId ==( long )PermissionsSectionEnum.Setup);







  var  depotPermission = 来自 o   WmsUser.WmsUserGroup.WmsUserGroupDepotCollection 
其中 o.DepotId == boundObject.DepotId
选择来自 p in o.WmsUserGroupDepotPermissionCollection
其中 p.SectionId ==( long )PermissionsSectionEnum。设置
选择 p
);







供将来参考: 101 Linq样本


Hi all,

I''m both new to C# and LINQ and finding it a steep hill to climb! I don''t know how to build this particular LINQ query and have broken it down into two separate parts to get it to work.
I''m sure I should be able to combine them but its beyond my knowledge at the moment.

Anyway, to the problem. I have WmsUser class which contains a WMSUserGroup class, and hanging from this is a WmsUserGroupDepotCollection, each member of which contains a permissions collection. Currently I''m retrieving the permissions class in two hits, first by fetching WmsUserGroupDepot from the WmsUserGroupDepotCollection using the DepotId contained within my boundObject. Then retreiving the WmsUserGroupDepotPermission from the WmsUserGroupDepot. Can anyone help me by showing how to combine the two statements?

Thanks in advance
Dave

WmsUserGroupDepot ugDepot=(
from o in ((App)App.Current).WmsUser.WmsUserGroup.WmsUserGroupDepotCollection
where o.DepotId == boundObject.DepotId
select o).SingleOrDefault();

WmsUserGroupDepotPermission depotPermission = 
(from o in ugDepot.WmsUserGroupDepotPermissionCollection
where o.SectionId == (long)PermissionsSectionEnum.Setup
select o).SingleOrDefault();

解决方案

Yes you can make this into one query, even a one liner. I''m not sure that you need to but you could do it any of these ways.

var depotPermission = WmsUser.WmsUserGroup.WmsUserGroupDepotCollection
                      .SingleOrDefault(x=>x.DepotId==boundObject.DepotId)
                      .WmsUserGroupDepotPermissionCollection
                      .SingleOrDefault(p=>p.SectionId== (long)PermissionsSectionEnum.Setup);



Or

var depotPermission = from o in WmsUser.WmsUserGroup.WmsUserGroupDepotCollection
                      where o.DepotId == boundObject.DepotId
                      select o.WmsUserGroupDepotPermissionCollection
                      .SingleOrDefault(x=>x.SectionId == (long)PermissionsSectionEnum.Setup);



Or

var depotPermission = from o in WmsUser.WmsUserGroup.WmsUserGroupDepotCollection
                      where o.DepotId == boundObject.DepotId 
                      select (from p in o.WmsUserGroupDepotPermissionCollection 
                            where p.SectionId == (long)PermissionsSectionEnum.Setup
                            select p
                            );




For your future reference: 101 Linq Samples


这篇关于结合LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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