none
Entity Framwork LINQ Selecting fields into typed class RRS feed

  • Question

  • I have the following LINQ to search for schools:

    var schoolLookUps = (from s in dbContext.Schools
                                          where s.Name.Contains(searchString) || s.Town.Contains(searchString)
                                          select new {Name = s.Name, ID = s.ID});
    

    Is there a way to pass Name and ID to my class LookUp without having to step through each record?

    Thanks in advance

    Tomas


    Tomas Ekenman www.luxuryspa.se www.redovision.se

    Wednesday, February 22, 2012 2:06 PM

Answers

  • Hi ekenman;

    The following will create a new instance of LookUp and initialize the two variables Name and ID.

    var schoolLookUps = (from s in dbContext.Schools
                         where s.Name.Contains(searchString) || s.Town.Contains(searchString)
                         select new LookUp {Name = s.Name, ID = s.ID});

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by ekenman Thursday, February 23, 2012 12:44 AM
    Wednesday, February 22, 2012 6:43 PM

All replies

  • Hi ekenman;

    The following will create a new instance of LookUp and initialize the two variables Name and ID.

    var schoolLookUps = (from s in dbContext.Schools
                         where s.Name.Contains(searchString) || s.Town.Contains(searchString)
                         select new LookUp {Name = s.Name, ID = s.ID});

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by ekenman Thursday, February 23, 2012 12:44 AM
    Wednesday, February 22, 2012 6:43 PM
  • Thank you. Thought I'd tried the obvious, but apparently not :).

    Tomas Ekenman www.luxuryspa.se www.redovision.se

    Thursday, February 23, 2012 12:45 AM
  •  

    Not a problem Tomas, glad I was able to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 23, 2012 4:15 PM