none
Linq to SQL Method syntax RRS feed

  • Question

  • I'm trying to rewrite the below query in the method syntax instead of query syntax.  There's a one to many relationship between results (child) and PersonsENT (parent) and a one to many relationship between PersonsENT (parent) and PersonRolesENT (child)

    results = (from r in results
       join pp in PersonsENT on r.OwnerId equals pp.DisplayPersonId
       join p in PersonRolesENT on pp.DisplayPersonId equals p.PersonId
       where p.RoleId == Author.AgencyRole
       select r );

     

    Thanks for your help


    Bj Bedford
    Thursday, September 9, 2010 8:48 PM

Answers

  • When joins are involved, query syntax is a lot more readable than method syntax, IMO, but here you go:

    results = results
      .Join(PersonsENT, r => r.OwnerId, pp => pp.DisplayPersonId, (r, pp) => new { r, pp })
      .Join(PersonRolesENT, pp => pp.pp.DisplayPersonID, p => p.PersonId, (o, p) => new { o, p })
      .Where(w => w.p.RoleId == Author.AgencyRole)
      .Select(s => s.o.r);


     
       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Rule based class and property naming, Compare and Sync model <=> DB, Sync SSDL <=> CSDL (EF4)
     huagati.com/L2SProfiler - Query profiler for Linq-to-SQL and Entity Framework v4
    • Proposed as answer by KristoferAEditor Saturday, September 11, 2010 4:50 AM
    • Marked as answer by crew54 Wednesday, September 15, 2010 2:11 PM
    • Unmarked as answer by crew54 Wednesday, September 15, 2010 2:14 PM
    • Marked as answer by crew54 Wednesday, September 15, 2010 2:23 PM
    Friday, September 10, 2010 3:01 AM
    Answerer

All replies

  • When joins are involved, query syntax is a lot more readable than method syntax, IMO, but here you go:

    results = results
      .Join(PersonsENT, r => r.OwnerId, pp => pp.DisplayPersonId, (r, pp) => new { r, pp })
      .Join(PersonRolesENT, pp => pp.pp.DisplayPersonID, p => p.PersonId, (o, p) => new { o, p })
      .Where(w => w.p.RoleId == Author.AgencyRole)
      .Select(s => s.o.r);


     
       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Rule based class and property naming, Compare and Sync model <=> DB, Sync SSDL <=> CSDL (EF4)
     huagati.com/L2SProfiler - Query profiler for Linq-to-SQL and Entity Framework v4
    • Proposed as answer by KristoferAEditor Saturday, September 11, 2010 4:50 AM
    • Marked as answer by crew54 Wednesday, September 15, 2010 2:11 PM
    • Unmarked as answer by crew54 Wednesday, September 15, 2010 2:14 PM
    • Marked as answer by crew54 Wednesday, September 15, 2010 2:23 PM
    Friday, September 10, 2010 3:01 AM
    Answerer
  • Hi Bj,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, September 14, 2010 8:55 AM
    Moderator
  • Thanks Kristofer for the help. That script worked with a small tweak to the line below.

          .Join(PersonRolesENT, p => p.pp.DisplayPerson, pp => pp.PersonId, (o,p) => {o,p})

    Which one is more efficient? Does it just depend on the problem you're trying to solve?

    Thanks again,

    BJ


    Bj Bedford
    Wednesday, September 15, 2010 2:23 PM
  • Which one is more efficient? Does it just depend on the problem you're trying to solve?


    What do you mean by which one is more efficient? Method syntax vs query syntax? No difference in runtime efficiency/performance, it is just language features; query syntax will be turned into the same method calls by the compiler. Query syntax is just more readable and easier to type for complex queries while method syntax is easier and more compact to type for simple queries.
     
       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Rule based class and property naming, Compare and Sync model <=> DB, Sync SSDL <=> CSDL (EF4)
     huagati.com/L2SProfiler - Query profiler for Linq-to-SQL and Entity Framework v4
    Thursday, September 16, 2010 12:53 AM
    Answerer