Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Difference between Select & Where in Linq

While implementing a condition in my project I got confused because of the use of Where & Select in Linq .

The scenario was

int _finalcount = ComponentFiles.Where(x => x.FileLocationTypeID.HasValue && x.FileLocationTypeID.Value == 4).Count();
 

int _finalcount = ComponentFiles.Select(x => x.FileLocationTypeID.HasValue && x.FileLocationTypeID.Value == 4).Count();

 My requirement was to find the count of File types with is satisfy the condition. Initially I have tried with select & it is always return count as ‘1’ instead of ‘0’. After a long analyses I found my foolish mistake.
I searched the difference between the two operations find the exact result.

Select is a projection, so what you get is the expression x=> x. FileLocationTypeID.Value == 4 evaluated for each element in ComponentFiles on the server. i.e. lots of true/false values (the same number as your original list). If you look the select will return something like IEnumerable<bool> (because the type of x.FileLocationTypeID.Value == 4 is a bool).
Where filters the results, returning an enumerable of the original type (no projection).

Simply say Where() is a filter.Select() selects a different piece of data

Linq Sample

Language Integrated Query, or LINQ for short (pronounced “link”), is a
set of Microsoft .NET Framework language enhancements and libraries
built by Microsoft to make working with simpler and more intuitive. LINQ provides a layer of programming abstraction between .NET languages and an ever-growing number of
underlying data sources.

C# 2.0 Approach
 
 Features of C# 2.0,being inline Delegates and Generic types. Its approach is to first sort the
collection by the LastName property using a comparison delegate, and then
it groups the collection by State property in a SortedDictionary collection. 

C# 2.0 code for grouping and sorting contact records—

List<Contact> contacts = Contact.SampleData();
// sort by last name
contacts.Sort(
delegate(Contact c1, Contact c2)
{
if (c1 != null && c2 != null)
return string.Compare(
c1.LastName, c2.LastName);
return 0;
}
);
// sort and group by state (using a sorted dictionary)
SortedDictionary<string, List<Contact>> groups =
new SortedDictionary<string, List<Contact>>();
foreach (Contact c in contacts)
{
if (groups.ContainsKey(c.State))
{
groups[c.State].Add(c);
}
else
{
List<Contact> list = new List<Contact>();
list.Add(c);
groups.Add(c.State, list);
}
}
// write out the results
foreach (KeyValuePair<string, List<Contact>>
group in groups)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group.Value)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}

 

LINQ Approach

LINQ to Objects, the LINQ features designed to add query functionality
over in-memory collections, makes this scenario very easy to implement.

C# 3.0 code for Linq

List<Contact> contacts = Contact.SampleData();
// perform the LINQ query
var query = from c in contacts
orderby c.State, c.LastName
group c by c.State;
// write out the results
foreach (var group in query)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}