I have a List inside that List one more List is there. How to check the id is present in any one of the List.
public class Users
{
public virtual Userid { get; set; }
public virtual List<UserCity> UserCities { get; set; }
}
And
public class UserCity
{
public virtual UserCityId { get; set; }
public virtual UserCityName { get; set; }
}
How to get the users which are having Userid =usercityid.
Linq query i tried like this
List<Users> lstusers = new List<Users>();
lstusers = lstusers
.Where(x=>x.UserCity.Any(w => w.UserCityId == w.Userid ))
.ToList();
If the users have usercityid with same userid i need to get those users.
CodePudding user response:
There are some errors in your query. Try this:
// create a dummy list for testing
var lstusers = new List<Users>
{
new Users {Userid = 1, UserCities = new List<UserCity>{ new UserCity { UserCityId = 1, UserCityName="xxx"} } }
};
// execute the linq-query
lstusers = lstusers.Where(x => x.UserCities.Any(w => w.UserCityId == x.Userid)).ToList();
// and print the number of users found
Console.WriteLine($"Number of users found: {lstusers.Count}");
