0

I faced this situation today and could not find on the web what I wanted.

Look at this code,

myCollection.Select(g => new ReportLine
{
    cds = (sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)) != null ? 
               sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).USER != null?
                  sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).USER.USER_FIRSTNAME : "": "")
                                + " " +
          (sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)) != null ?
               sectors.Where(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).FirstOrDefault().USER != null?
                  sectors.Where(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).FirstOrDefault().USER.LASTNAME : "" : "")
});

Basically "myCollection" is a list of a deep class and this request goes into it extract a first and last name and put them in an other class while checking if there is no null values.

You noticed to achieve that I checked the same thing 6 times:

sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE))

(probably painful for performance)

Does a way exists to "store" the value within the linq expression ? something like this:

myCollection.Select(g => new ReportLine
{
    cds = ((var tmp =sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE))) != null ? 
               tmp.USER != null?
                  tmp.USER.USER_FIRSTNAME + "" + tmp.USER.LASTNAME: "": "")
});

5 Answers 5

2

I think you're probably going to have to change to long-hand LINQ to make it happen, but you can use LINQ's let clause to assign variables in the middle of an expression.

Sign up to request clarification or add additional context in comments.

3 Comments

What is long-hand LINQ? I see no problem in using the fluent syntax.
See the link in the answer for an example. It's a more verbose, SQL-ish way to write the same expressions.
I know what the regular linq syntax is, and the let clause, I just thought you meant something else when you wrote long-hand LINQ, since I've never heard it before. But why did you thought he had to change to that syntax?
1

Try this:

myCollection.Select(g => { 
    var sect_code = g.Contract.Station.Sector.SECT_CODE;
    var sector = sectors.FirstOrDefault(s => s.SECT_CODE.Equals(sect_code));
    var firstName = sector != null ? 
        (sector.USER != null ? sector.USER.USER_FIRSTNAME: "") : 
        "";
    var lastName = sector != null ? 
        (sector.USER != null ? sector.USER.LASTNAME : "") : 
        "";
    return new ReportLine
    {
        cds = string.Format("{0} {1}", firstName, lastName)
    };
});

2 Comments

Pretty sure that EF won't be able to properly translate that.
@Servy, why not? That is standard linq? Do you mean because it won't be able to lookup Contract then Station and Sector? If you ask me that is not the above linq that is the problem, rather it is EF, if used, and that he has a really nested structure in the expression.
-1

You could extract the logic in the Select statement to a separate method, and call that inside the Select statement?

Something like...

private string Foo(Sector input)
{
    var tmp = g.SECT_CODE.Equals etc etc
    return tmp.USER != null ? etc etc
}

...

myCollection.Select(g => Foo(g))

8 Comments

If you make this change then EF won't be able to parse the code to translate it into SQL.
Was it specified that this was a SQL call?
The question is tagged as EF.
You're not necessarily interacting with SQL even if you're using EF.
But the expressions do need to be parseable, and this isn't going to allow for that.
|
-1

myCollection.Select(g => { var temp=sectors.FirstOrDefault(); ...

return new ReportLine(){..};

})

Comments

-1

This should be what you are looking for:

myCollection.Select(g => new ReportLine
{
    cds =  (from s in sectors
            where s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE) && s.USER != null
            select string.Format("{0} {1}", s.USER.USER_FIRSTNAME, s.USER.USER_LASTNAME)).FirstOrDefault() ?? " "
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.