LINQ and ToList when querying multiple sources

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc S

    #1

    LINQ and ToList when querying multiple sources

    var qry = from s in dbCxt.Services
    from j in s.Jobs
    where s.ID == 200
    select new { s, j };


    I want to call qry.ToList() but with the list containing anonomyous
    types I don't know how to create a List object to support that. I want
    to get all records in a List so that I only have to make 1 call to the
    database. Is this even possible?
  • Marc Gravell

    #2
    Re: LINQ and ToList when querying multiple sources

    Yes; use ToList()

    i.e. either:

    var list = qry.ToList();

    or

    var list = ( from s in dbCxt.Services
    from j in s.Jobs
    where s.ID == 200
    select new { s, j }).ToList();

    It will be a list of anon-types, but that is fine.

    Marc

    Comment

    • Martin Honnen

      #3
      Re: LINQ and ToList when querying multiple sources

      Marc S wrote:
      var qry = from s in dbCxt.Services
      from j in s.Jobs
      where s.ID == 200
      select new { s, j };
      >
      >
      I want to call qry.ToList() but with the list containing anonomyous
      types I don't know how to create a List object to support that. I want
      to get all records in a List so that I only have to make 1 call to the
      database. Is this even possible?
      Use 'var' again e.g.
      var list = qry.ToList();

      --

      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/

      Comment

      • Marc S

        #4
        Re: LINQ and ToList when querying multiple sources

        On Apr 9, 10:48 am, Marc Gravell <marc.grav...@g mail.comwrote:
        Yes; use ToList()
        >
        i.e. either:
        >
        var list = qry.ToList();
        >
        or
        >
        var list = ( from s in dbCxt.Services
                   from j in s.Jobs
                   where s.ID == 200
                   select new { s, j }).ToList();
        >
        It will be a list of anon-types, but that is fine.
        >
        Marc
        Exactly waht I need. I am in the process of discovering LINQ to
        rewrite a database access library. Thanks to all!

        Comment

        Working...