0

I am trying to convert a Method Syntax to Query Syntax.

Class Structure is nested like this:

  1. Property
  2. PropertyParty
  3. Party
  4. PartyMailingAddress
  5. PropertyMailingAddress

It seems MethodSyntax (first one), is bringing less rows grouped together compared to Query Syntax, bringing more rows .

How do I fix second Query syntax to be equivalent to Method Syntax?

var result = db.Property.Include(pm => pm.PropertyParty)
                    .Include(pm => pm.PropertyParty)
                    .ThenInclude(x => x.Party)
                    .ThenInclude(x => x.PartyMailingAddress)
                    .ThenInclude(x => x.PropertyMailingAddress)
                    .ToList();

var testingResult = (from property in db.Property
                     join propertyParty in db.PropertyParty
                        on property.PropertyId equals propertyParty.PropertyId
                     join party in db.Party
                        on propertyParty.PartyId equals party.PartyId
                     join partyMailingAddress in db.PartyMailingAddress
                        on party.PartyId equals partyMailingAddress.PartyId
                     join propertyMailingAddress in db.PropertyMailingAddress
                        on partyMailingAddress.PartyMailingAddressId equals propertyMailingAddress.PartyMailingAddressId
                     select property).ToList();

*If there is no equivalent, could I grab the query results, and group them to be similar to Method Syntax?

The Query syntax answer should Not contain ThenInclude

Currently using Net Core 2.2

  • 3
    Please do not use tags in your question titles. The [tagging help page](https://stackoverflow.com/help/tagging) strongly advises against it: _"The only time you should use tags in your title is when they are organic to the conversational tone of the title."_ – ProgrammingLlama May 27 '20 at 08:21
  • hi, I was looking at these links https://stackoverflow.com/questions/18547354/c-sharp-linq-find-duplicates-in-list?r=SearchResults&s=4|144.4877 https://stackoverflow.com/questions/6414816/is-there-a-c-sharp-linq-syntax-for-the-queryable-selectmany-method?r=SearchResults&s=14|101.6082 –  May 28 '20 at 03:04
  • 1
    For one, this site's rules change over time, so perhaps the no tags in titles thing wasn't part of the tagging guidelines back then. Perhaps the questions just slipped through the net - many do. Basing your usage of the site in contradiction of the actual official guidelines and justifying it with old questions seems similar to asking the officer not to give you a speeding ticket because the speed limit used to be higher some 10 years ago. – ProgrammingLlama May 28 '20 at 03:07
  • Here is a meta post about tags in titles: https://meta.stackexchange.com/q/19190/686592 – Modus Tollens May 28 '20 at 03:11
  • 1
    Look at this from an alternative perspective as well: what benefit does this give you? If someone is interested in answering questions about C# or LINQ they won't do a text search for C# or LINQ. Like me, they will open the [C# tag page](https://stackoverflow.com/questions/tagged/c%23) so that they can see new questions tagged C#. That's how I found your question. It doesn't increase your visibility at all, if that's what you're thinking. – ProgrammingLlama May 28 '20 at 03:12
  • ok, yeah, that was my buddy Fred was telling me, he was saying it helps in points or something, he has 20k –  May 28 '20 at 03:13
  • ok anyways, problem solved, I made it in reference to question, following example https://stackoverflow.com/questions/6414816/is-there-a-c-sharp-linq-syntax-for-the-queryable-selectmany-method –  May 28 '20 at 03:14

2 Answers2

2

There is not special LINQ syntax for Include. So the equivilent is not a bunch of joins. It's

var q = from p in db.Property
                    .Include(pm => pm.PropertyParty)
                    .Include(pm => pm.PropertyParty)
                    .ThenInclude(x => x.Party)
                    .ThenInclude(x => x.PartyMailingAddress)
                    .ThenInclude(x => x.PropertyMailingAddress)
        select p;

var testingReqult = p.ToList();
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • so are you saying there is no equivalent? you just placed my first above, is there no linq to sql alternative? thanks I couldn't grab the linq to sql results, and then group condense them?? –  May 27 '20 at 08:33
  • I couldn't grab the linq to sql results, and then group condense them?? –  May 27 '20 at 08:35
0

The reason why you're seeing more rows on the Query Syntax is because there you're doing joins, while the Method one is doing includes which just loads the related entities (check this).

If you really need to combine it with a query you could do something like this:

(from property in db.Property 
 select property)
    .Include(pm => pm.PropertyParty)
    .Include(pm => pm.PropertyParty)
    .ThenInclude(x => x.Party)
    .ThenInclude(x => x.PartyMailingAddress)
    .ThenInclude(x => x.PropertyMailingAddress)
    .ToList();

If you're looking for a group by because you want to stick with the query syntax, it would be something like this (but to me all this work doesn't seem to be worth it)

var testingResult = (from property in db.Property
                     join propertyParty in db.PropertyParty
                        on property.PropertyId equals propertyParty.PropertyId
                     join party in db.Party
                        on propertyParty.PartyId equals party.PartyId
                     join partyMailingAddress in db.PartyMailingAddress
                        on party.PartyId equals partyMailingAddress.PartyId
                     join propertyMailingAddress in db.PropertyMailingAddress
                        on partyMailingAddress.PartyMailingAddressId equals propertyMailingAddress.PartyMailingAddressId
                     group property by new { property.Field1, x.Field2, ... /* Group by all the property fields */ } into grouped
                     select new Property 
                     {
                        Field1 = grouped.Key.Field1,
                        Field2 = grouped.Key.Field2,
                        ... /* Manually populate all the properties */
                        PropertyParties = grouped.Select(x => x.PropertyParties),
                        ... /* Manually populate all the related entities */
                     }).ToList();
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
devcrp
  • 1,323
  • 7
  • 17
  • I am trying to avoid using theninclude, this will not answer the question –  May 27 '20 at 08:37
  • I'd say there's no equivalend to `Include` using query syntax. I'm pretty sure the only way to do Includes is by calling the `Include()` method like that. I tried to illustrate how to combine both in case the query was more complex. – devcrp May 27 '20 at 08:40
  • I couldn't grab the linq to sql results, and then group condense them?? –  May 27 '20 at 08:48
  • @Artportraitdesign1 just edited my answer, that's the approach using a group by, I'd just use the method one though tbh. – devcrp May 27 '20 at 09:00