tags:

views:

53

answers:

2

what is shorthand for this or should i not do this? do i call a method to check?

 bool assign=value;
 objectsList = from o in objects select new {
                              id=o.id,
                              name=o.name or "" (if assign==false),

                                }
+1  A: 
bool assign=value;
var objectsList = from o in objects select new 
                  {
                      id = o.id,
                      name = assign ? o.name : String.Empty 
                  };
Yuriy Faktorovich
+3  A: 

Do you mean something like:

bool assign=value;
var objectsList = from o in objects select new {
    id=o.id,
    name=(assign ? o.name : String.Empty)
};

PS: Be wary of deferred execution - check on assign will occur at the point you use it rather than when you declare the select statement.

Tim Schneider
Good point, but you can be more general. The check occurs where it is used regardless of whether "objects" is IQueryable or not. "objectsList" is just an object that represents the query; it doesn't execute until you do something to it to make it run. So the execution of the check during the projection will always happen at that time, using whatever value happens to be in "assign" at that time.
Eric Lippert
I didn't realize that happened even for IEnumerable's implementation of Select. I did a few tests and you're definitely right. Thanks for the heads up.
Tim Schneider