I am using a two LINQ queries(nested)
Here’s what I want my query to achieve:
In my inner query
- Retrieve a collection of
UserLocationobjects using two conditions
In my outer query
- Retrieve a filtered collection of
Userobjects whereUser.UIDmatches the propertyUserLocation.UIDof eachUserLocationobject from the collection in the inner query.
I’m almost there code-wise, I’m just missing the final step — I don’t know how to get the outer query to enumerate through the UserLocation collection and match the UID.
In my code I have two queries, the top one is a working example of getting the FullName property of a User object using the inner query and the conditions I need(as well as matching UID).
The second query is the one I am having trouble with. What am I missing?
ownerLiteral.Text =
Users.First(u => u.UID.Equals(
UserLocations.First(s => s.IsOwner == true && s.LID.Equals(building.LID)).UID)).FullName;
var propertyteam =
Users.Where(c => c.UID.Equals(
UserLocations.Where(x => x.IsPropertyTeam == true && x.LID.Equals(building.LID))));
Edit: Fixed the problem
I had forgotten that UserLocations was a member of Users — I shortened down my query and used .Any to select the UserLocations members that fit my conditions, then just return the User.
In the first query I return the FullName for the User object.
In the second query I now return a collection of User objects that fit the conditions.
For those that are interested I bind the second query to a DataList and then evaluate for their FullName in the user control.
ownerLiteral.Text =
Users.First(
u => u.UserLocations.Any(
ul => ul.IsOwner == true && ul.LID.Equals(building.LID))).FullName;
var propertyteam =
Users.Where(
u => u.UserLocations.Any(
ul => ul.IsPropertyTeam && ul.LID.Equals(building.LID)));
Your class relationships are confusing me, but I think your problem is that you’re trying to treat a collection of
UserLocationobjects (Where()returns anIEnumerable) as a singleUserLocationI think this might do it:
Edit, based on further information:
So maybe this is what you’re looking for?
Assuming the UID member of a UserLocation is a whole User object, and not just some int ID for a User.