Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ What does 'select new' do?

Tags:

c#

linq

I've built the following LINQ query

var activeMembers = from m in context.py3_membershipSet
                    join c in context.ContactSet on m.py3_Member.Id equals c.ContactId 
                    where m.statuscode.Value == 1
                    orderby m.py3_name
                    select m;

But I've since seen an example formatted as such:

var contacts =
(
    from c in xrm.ContactSet
    join a in xrm.AccountSet on c.ParentCustomerId.Id equals a.Id
    where a.Name == "Acme Pty Ltd"
    select new
    {
        Name = c.FullName,
        DOB = c.BirthDate,
        Gender = (c.FormattedValues.Contains("gendercode") ? c.FormattedValues["gendercode"] : "Ambiguous")
    }
);

(I realise this is a different set of data) What does the inclusion of the 'select new' actually do in this case?

What are the benefits of it over my example in the first code block?

I realise some might find this a tedious question, but I a want to learn LINQ and need to learn it fast. But I also don't want to run something -that I don't fully understand- on a clients live CRM

like image 620
Phill Healey Avatar asked Oct 20 '25 06:10

Phill Healey


1 Answers

As previous answers have noted both methods return an anonymous type. To fully answer your question though: "What are the benefits of the second statement over the first?"

The first statement returns all of the fields of m as-is. If you have 7 "columns" then activeMembers will contain all of them and whatever data they contain.

In the second statement you're projecting the results into a custom anonymous object that has only 3 fields. Not only that but you can perform logic on the "source fields" before storing them in the anonymous object. This gives you much of the flexibility of storing them in a container class without you actually having to define that class in code.

You could also do select new SomeClass { } which would project your results into a predefined class container.

The following pseudo-code may or may not be helpful in understanding the difference:

var myQuery = from p in someContext.someTable
              group p by p.someField into g
              select new MyContainer {
                  Field1 = g.Sum(a => a.field1)
                  Field2 = g.Max(a => a.field2)
              };

myQuery in the above is now a collection of MyContainer. If I had omitted the class MyContainer then it would be a collection of an Anonymous Type containing the fields that I specified. Obviously, the difference here is that MyContainer has to be defined elsewhere in your code whereas the anonymous class is built/defined for you at compile time. In:

var myQuery = from p in someContext.someTable
              select p;

myQuery is an anonymous class containing all of the fields and their values in someTable.

like image 91
Kittoes0124 Avatar answered Oct 22 '25 20:10

Kittoes0124



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!