Search code examples
c#linqlinq-to-objects

Linq query with curly braces for anonymous action/function body


I have this entities:

public class Parent: AllDependant
{
    /*Properties goes here*/
}

public class Children: AllDependant
{
    /*Properties goes here*/
}

Then I have allDependants variable with typeof List<AllDependant> which will keep some of parents and childrens entitiy in mix.

Later on, I want to select from them and do something like this:

var selectedDependantInfos = allDependants
        .Select(dependant =>
        {
            if (dependant is Parent)
            {
                var parent = dependant as Parent;
                return new { Name = parent.Name, SomeSpecialInfo = parent.ParentInfo };
            }
            else
            {
                var child = dependant as Children;
                return new { Name = child.Name, SomeSpecialInfo = child.ChildInfo }
            }
        });

Note the specific property for child and parent require me to cast and get the property to a new model for UI display which is not entity concern. I cannot put the special property in AllDependant base class because I need to refactor the property name on so many files including *.ascx which is troublesome. However it done by using the Linq Select extension method above but I just thinking of this:

Question: How can I do the same in Linq Query?

This will give error on the select keyword and the curly braces:

var selectedDependantInfos = from dependant in allDependants
                            select
                            {
                                /* the same if statement goes here */
                            }

Solution

  • You would use the conditional operator and get something like

      from dependant in allDependants             
      select dependant is Parent 
             ? new { Name = (dependant as Parent).Name,  /* Parent fields */ }
             : new { Name = (dependant as Children).Name, /* Child fields */ }
    

    But as you see that is not a great improvement. There's no convenient place to do the type-cast.

    The better option would seem to move the Name and SpecialInfo properties to a base class (AllDependant or a special intermediate class).