开发者

LINQ Abbreviation Tip

I've a LINQ query like below:

foreach (var property in from property in properties w开发者_StackOverflow社区here property.Name != "Type" select property)
{
}

how would you go about making this statement more concise without using the actual extension method which looks unattractive (i.e. without using .Where like: foreach (var property in properties.Where(...)).


You cant really..

You could put the query into a separate line.

var selectedProperties = from property in properties 
                         where property.Name != "Type" 
                         select property;

foreach (var property in selectedProperties)
{
}

Or you could factor the query out into a separate method if it is really huge.

foreach ( var property in ComplexSelectionOfProperties () )
...

But really I would say the exention method in this case is much neater. Its only when the queries get more complex and involve joins that the query syntax becomes tidier. (IMHO)


Beauty is always in the eye of the beholder :)

However in such a case I would go create a method that filters non-Type properties and iterate over its results.

Something like this:

IEnumerable<IProperty> GetNonTypeProperties(IEnumerable<IProperty> properties)
{
    return (from property in properties where property.Name != "Type" select property);
}

void foo()
{
    foreach (var property in GetNonTypeProperties(properties))
    {
    }
}


The lack of conciseness comes precisely from the sql style syntax : using a "dot" notation you will sensibly shorten your expression :

foreach (var property in properties.Where(property => property.Name != "Type"))
{
}

If you want to shorten the longest part which is obviously the boolean test, you have to put it elsewhere.

Either in the foreach loop itself :

foreach (var property in properties)
{
    if(property.Name != "Type")
    {
        ...
    }
}

Either if a separate function :

foreach (var property in properties.Where(IsNotType))
{
}

//and farther :

bool IsNotType(Property p)
{
    return property.Name != "Type";
}

But anyway you want to perform a loop with a test on each element, so in a way or another you will have to code that and it will take a minimum amount of characters.


Just in case that you don't like the lambda expression, not the extension method itself, you can make your own extension method with query inside, like this:

public static IEnumerable<Property> PropertiesExceptType(this IEnumerable<Property> properties) {
    return from property in properties 
           where property.Name != "Type" 
           select property;
}

and use it:

foreach(var property in properties.PropertiesExceptType()) {
    // ...
}

The good thing about encapsulating your query in separate method is that you can debug the method with loop and change the code on the fly (VS won't let you do this if you have a linq query right inside this method).


I honestly don't see anything wrong with var propery in properties.Where(), it's much better than any query expression in this context IMO. But if you want to stick with your query, at least introduce a variable:

var filteredProperties = from property in properties 
                         where property.Name != "Type" 
                         select property;
foreach(var property in filteredProperties)
{
   // ...
}

People who will read and debug it later will thank you. But I still think that extension method is the way to go here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜