find the value of Ienumerable in List<>

Jey 1 Reputation point
2021-12-07T17:08:47.177+00:00

hello there

I have a variable Ienumerable<Model1> called ienuResult it has some data like CityName, WorkList(its nesterd array; this nested array has Name, Age)
another variable as List<Model2> called lstWhere it has some data like Name, Sex

now I want to know, Is lstWhere-Name is exist in ienuResult-WorkList-Name; If exist then find CityName, Age(from WorkList) of ienuResult.

May i know how do i do this? I used Contains but, it gave me error as "'IEnumerable<string>' does not contain a definition for 'Contains' and the best extension method overload "

below is my sample model code

public class Travel
{
public string CityName { get; set; }
public WorkList Worklist { get; set; }
}
public class WorkList
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Adult
{
    public string Name { get; set; }
    public string Sex { get; set; }
}

below code is from controller

var adultList = new List<Adult>();
adultList.Add(new Adult { Name = "Mercy", Sex = "Male" });
adultList.Add(new Adult { Name = "Mary", Sex = "FeMale" });
adultList.Add(new Adult { Name = "Jiber", Sex = "Male" });
adultList.Add(new Adult { Name = "Derry", Sex = "FeMale" });
adultList.Add(new Adult { Name = "Crown", Sex = "Male" });

        var travelList = new List<Travel>();
        travelList.Add(new Travel { CityName = "Lisbon", Worklist = new WorkList { Age = 13, Name = "John" } });
        travelList.Add(new Travel { CityName = "Porto", Worklist = new WorkList { Age = 14, Name = "Mary" } });
        travelList.Add(new Travel { CityName = "Faro", Worklist = new WorkList { Age = 15, Name = "Mic" } });
        travelList.Add(new Travel { CityName = "Albufeira", Worklist = new WorkList { Age = 16, Name = "John" } });

        var ienuResult = from travel in travelList
                                 select travel;

        var lstWhere = (from adult in adultList
                          select adult).ToList();

        var isExist = from row in ienuResult
                      where row.Worklist.Name.Any(x=> (from adult in lstWhere select adult.Name).Contains(x))
                      //where row.Worklist.Name.Intersect( (from adult in lstWhere select adult.Name).ToList())
                      //where row.Worklist.Name.Contains( (from adult in lstWhere select adult.Name).ToList())
                      select row;

Thanks
J

Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-12-07T18:59:17.963+00:00

    Firstly thank you for providing a sample set of code for us to use. It speeds things up.

    If I understand your question correctly you want to get the adults who are traveling along with the city(s) and age(s).

    var adultsWithTravel = from adult in adultList
                           join travel in travelList on adult.Name equals travel.Worklist?.Name
                           select new { Adult = adult, City = travel.CityName, Age = travel.Worklist.Age };
    

    For your sample set it would return back 1 entry as only Mary is in the list of adults. However if we were to add John to the list of adults we'd get back 3 entries (1 for Mary and 2 for John). I'm ignoring the fact here that John is probably 2 different people given that the ages are different. But you most likely want each adult with their travel grouped together so we can adjust the query to handle that instead by grouping on the worklist name (and optionally age) first.

    var adultsWithTravel = from travel in travelList
                           group travel by travel.Worklist?.Name into g
                           join adult in adultList on g.Key equals adult.Name
                           select new { Adult = adult, Travel = g.ToArray() };
    

    Now you get back 2 results, 1 for Mary and 1 for John. Each result has a Travel property that is an array of the place(s) they want to travel. To me this is the easiest approach but if you really just want the city and age then you can adjust the g.ToArray() to instead select just the subset of data.

    var adultsWithTravel = from travel in travelList
                           group travel by travel.Worklist?.Name into g
                           join adult in adultList on g.Key equals adult.Name
                           select new { Adult = adult, Travel = g.Select(x => new { City = x.CityName, Age = x.Worklist.Age }).ToArray() };
    
    0 comments No comments

  2. Anonymous
    2021-12-08T03:08:33.653+00:00

    Hi @Jey ,

    now I want to know, Is lstWhere-Name is exist in ienuResult-WorkList-Name; If exist then find CityName, Age(from WorkList) of ienuResult.

    May i know how do i do this? I used Contains but, it gave me error as "'IEnumerable<string>' does not contain a definition for 'Contains' and the best extension method overload "

    You can try to use the Any() method, code like this:

             var result = travelList.Where(c => adultList.Any(a => a.Name == c.Worklist.Name))  
                .Select(c=> new { CityName =c.CityName, Name = c.Worklist.Age}).ToList();  
    

    The result:

    155680-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.