Volodymyr Machula
Volodymyr Machula

Reputation: 1594

What is the benefit of using IQueryable and LINQ queries?

I have a project where was realized own configuration classes:

IconSizesConfigSection: ConfigurationSection
IconSizesCollection: ConfigurationElementCollection
IconSize: ConfigurationElement

In Config class exists this property:

public IQueryable<IconSize> IconSizes
    {
        get 
        {
            IconSizesConfigSection configInfo = (IconSizesConfigSection)ConfigurationManager.GetSection("iconConfig");
            return configInfo.IconSizes.OfType<IconSize>().AsQueryable<IconSize>(); 

        }
    }

IconSizes property returns IconSizesCollection which derives from ConfigurationElementCollection. In turn ConfigurationElementCollection derives from ICollection, IEnumerable.

In some another class I have such code:

var previewIconSize = Config.IconSizes.FirstOrDefault(c => c.Name == "AvatarSize");

Why in such case uses Deffered Execution? Why initially it uses AsQueryable<IconSize>() for collection and then uses LINQ and Deffered Execution?

Is there any benefits compared with using simple List?

Upvotes: 2

Views: 735

Answers (3)

Paulo Morgado
Paulo Morgado

Reputation: 14846

Both IEnumerable and IQueryable use deferred execution. The difference is that IQueryable is used to cross boundaries like database queries, entity framework queries or OData queries.

When an IQueryable is iterated over, the query is translated to the remote provider's idiom and executed there. When the response is received from the remote provider, it is translated to a local object representation.

Upvotes: 1

softwarebear
softwarebear

Reputation: 451

Deferred Execution is good because your user may never use the result set and hence there would have been no point querying the data source.

There may be some LINQ methods your user can't use unless they cast the result to IQueryable which means you might restrict what they can do, or force them to cast/copy the list into something more useful.

If you use a List, then you're hard coding your solution to a List, do you care what the implementation of the collection is, does your user ... probably not as long as it supports the necessary interfaces.

Upvotes: 0

smartcaveman
smartcaveman

Reputation: 42246

In these case, there is no practical benefit. Using IQueryable is helpful for cases when query rewriting/translation will optimize performance. You will actually incur decreased performance in the provided example.

One example of using IQueryable in a helpful way is the significant performance increase gained when lazily translating and evaluating queries against a database or web service. This will perform significantly better than the alternative of pulling massive result sets and applying query logic in active memory with a "simple List".

The way you can tell that using the IQueryable in your case is detrimental is that the collection is already loaded into memory, when you begin the query.

Upvotes: 1

Related Questions