Click here to Skip to main content
15,797,923 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
C#
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();
Posted
Updated 27-Feb-13 5:12am
v2

1 solution

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.

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


Or

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


Or
C#
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900