Take the 2-minute tour ×

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 UserLocation objects using two conditions

In my outer query

  • Retrieve a filtered collection of User objects where User.UID matches the property UserLocation.UID of each UserLocation object 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 = 
            u => u.UserLocations.Any(
                ul => ul.IsOwner == true && ul.LID.Equals(building.LID))).FullName;

var propertyteam = 
            u => u.UserLocations.Any(
                ul => ul.IsPropertyTeam && ul.LID.Equals(building.LID)));
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Your class relationships are confusing me, but I think your problem is that you're trying to treat a collection of UserLocation objects (Where() returns an IEnumerable) as a single UserLocation

I think this might do it:

var propertyteam = LoggedInUser.ExtranetUser.Company.Users
  .Where(c => c.UID.IsPropertyTeam == true && c.UID.LID.Equals(building.LID));

Edit, based on further information:

So maybe this is what you're looking for?

var uidsWhoArePartOfThePropertyTeamForThisBuilding
  = UserLocations.Where(x => x.IsPropertyTeam && x.LID == building.LID)
  .Select(x => x.UID);

Assuming the UID member of a UserLocation is a whole User object, and not just some int ID for a User.

share|improve this answer
Ah sorry about that, I'll clean up the code a little bit. I do want to return a collection of UserLocations though. UserLocations is a "linked" database between Locations and Users and specifies whether they are connected. Each LID(location id) is matched to a UID(user id) and then has the properties IsOwner or IsPropertyTeam. User only contains UID. –  Matt Foxx Duncan Jul 6 '12 at 19:32
See my edit above. Are you sure you want a collection of UserLocations? What are you going to do with it? o_O –  mo. Jul 6 '12 at 19:47
Close enough, I'm editing my question to include the answer I found. I had forgotten that UserLocations was also a property of Users. Thanks for the help! –  Matt Foxx Duncan Jul 6 '12 at 19:49
Glad it helped :) –  mo. Jul 6 '12 at 20:14

To write a T-SQL type IN clause, in LINQ you would use Contains. This would be where I would start:

    var userlocations = LoggedInUser.ExtranetUser.UserLocations.Select(ul => ul.UID);
    var propertyteam = LoggedInUser.ExtranetUser.Company.Users.Where(userlocations.Contains(u => u.UID));
share|improve this answer

Your Answer


