none
Advice on Select part RRS feed

  • Question

  • Hi

    I have a LINQ statement as below;

    Dim dataSource = Context.Applicants.Include("ApplicantNotes").Where(Function(p) p.JobType = "Waiter")

    How can I add a Select part to it so only ID and Lastname are returned from Applicants entity, and only Notes field is retuned from ApplicantNotes related entity?

    Many Thanks

    Regards




    Friday, August 19, 2011 7:26 PM

Answers

  • Hy Y a h y a;

    Because the relationship is 1 to many the Notes field in the resultset will be a collections of IEnumerable(Of Strings) if the data type of Note is a String type.

    Dim dataSource = Context.Applicants.Include("ApplicantNotes") _
          .Where(Function(p) p.JobType = "Waiter") _
          .Select(Function( a ) New With { _
          .ID = a.ID, _
          .Lastname = a.Lastname, _
          .Notes = a.ApplicantNotes.Select( Function(n) n.Note ) _
         }
    

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Y a h y a Friday, August 19, 2011 10:48 PM
    Friday, August 19, 2011 10:39 PM

All replies

  • Hy  Y a h y a;

    I am assuming a 1 to 1 relationship between Applicants and ApplicantNotes.

    Dim dataSource = Context.Applicants.Include("ApplicantNotes") _
               .Where(Function(p) p.JobType = "Waiter") _
               .Select(Function( a ) New With { _
               .ID = a.ID, _
               .Lastname = a.Lastname, _
               .Notes = a.ApplicantNotes.Notes _
             }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, August 19, 2011 8:48 PM
  • Hi

    Thanks. The relationship is 1-N between and Applicants and ApplicantNotes. Would it be any different?

    Thanks

    Regards

    Friday, August 19, 2011 9:04 PM
  • Hi

    I tried the code. Unfortunately no fields under related entity ApplicantNotes show up as per image below;
     

    Even though fields do exists under entity ApplicantNotes in entity model;

    Regards

    Friday, August 19, 2011 10:12 PM
  • Hy Y a h y a;

    Because the relationship is 1 to many the Notes field in the resultset will be a collections of IEnumerable(Of Strings) if the data type of Note is a String type.

    Dim dataSource = Context.Applicants.Include("ApplicantNotes") _
          .Where(Function(p) p.JobType = "Waiter") _
          .Select(Function( a ) New With { _
          .ID = a.ID, _
          .Lastname = a.Lastname, _
          .Notes = a.ApplicantNotes.Select( Function(n) n.Note ) _
         }
    

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Y a h y a Friday, August 19, 2011 10:48 PM
    Friday, August 19, 2011 10:39 PM
  • Hi

    Many thanks. That was very helpful.

    Regards

    Friday, August 19, 2011 10:49 PM
  •  

    Not a problem, 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".
    Friday, August 19, 2011 10:52 PM