locked
LINQ Expressions query RRS feed

  • Question

  • User1094877758 posted

    I'm trying to generate a List<Session> from the linq query below.  But it's coming back with List<IEnumberable<Session>>.  

    var sessions = events.Select(e => e.sessions.Where(s => s.name == searchTerm)).ToList();

    How do I get it to return a List<Session>?

    Here is the schema for the event class:

     public class Event
     {
            public int id { get; set; }
            public string name { get; set; }
            public string date { get; set; }
            public string time { get; set; }
            public double price { get; set; }
            public string imageUrl { get; set; }
            public Location location { get; set; }
            public List<Session> sessions { get; set; }
            public string onlineUrl { get; set; }
     }



    Thursday, June 22, 2017 7:22 PM

All replies

  • User1068175894 posted
    var sessions = events.SelectMany(e => e.sessions.Where(s => s.name == searchTerm)).ToList();

    Thursday, June 22, 2017 9:13 PM
  • User-707554951 posted

    Hi Wmincic

    As rds80 said, you could use selectmany method.

    You also could add some line of code :

     var sessions = conext.Events.Select(w => w.sessions.Where(s => s.Sessionname == "Sessionname3"));
    //add the following code List<Session> listresult = new List<CaseTest.Session>(); foreach (List<Session> sessionlist in sessions.ToList()) { foreach (Session session in sessionlist) { Response.Write(session.id); Response.Write(session.Sessionname); listresult.Add(new Session { id = session.id, Sessionname = session.Sessionname }); } }

    Best regard

    Cathy

    Friday, June 23, 2017 9:42 AM