Basic LINQ Queries From a Collection

I wanted to touch upon some of the capabilities with LINQ.  I'm getting into a project that will be using LINQ, and so I've been looking at its capabilities and trying to discover all that it can do.  Here are some of the basic LINQ queries that you can perform.  If you don't know what LINQ is, you can find out about it by googling LINQ.  Basically, LINQ takes some of the capabilities of SQL for databases, and .NET-ified it by allowing you to query collections, XML files, and databases through code, in a very simple approach.

LINQ is handy because you can parse through a collection easier.  Before LINQ, if you had a collection of business objects, which the object has a category property, to get a distinct list of categories meant iterating through the list and adding the category name to another temporary collection.  With LINQ, this is a snap.  Below is an example; the collection being queried is a ResourceItemCollection that has ResourceItem objects.  This object type has a category property that is a struct, with the underlying string value being categoryname.

The following is the LINQ query to get a distinct list of category names:

var categories = (from resx in resources
                       
select resx.Category).Distinct();

That's it.  That's all it takes to get a distinct list of categories.  Let me break down the query.  LINQ moves the select list to the bottom of the syntax, though the from and join statements are in the same order, as well as the ordering.  Resx is like a table alias, naming the resources collection (which is an enumerable list) as resx for future use in the query.  The select clause specifies retrieving the Category property only.  In addition, I could have specified "select resx" without the property, and this would return the entire ResourceItem object.

The Distinct() method, one of many specialty methods, is the method used to get a distinct list of items, which is returned to a variable of type variant.  What is "var", you may ask?  Var allows you to not know what the underlying result will be; however, the framework determines what the underlying return type is, so it is still strongly-typed in VS 2008, along with full intellisense.  In a sense, an anonymous type with a property of the Category struct is created and returned as a list, which is how it can be strongly typed.  Below is how the categories are used:

foreach (var category in categories)
{
    Expander expander = new Expander();
    expander.Header = category.CategoryName;
}

Using var in the iterator allows me to dynamically get the category name, because the framework knows the resulting type was a collection of category structs.

You can also use a where clause, as shown below, which in this case the list of items matching a particular category are returned:

var items = from resx in resources
                
where resx.Category = category
                
select resx;

Notice the where clause; it uses the resx designator to reference the ResourceItem object being compared in the where clause, to the string category listed above.  Also notice the resx designation in the select statement, which selects the object as a whole.  Also note, that if you want to select multiple properties, you can do so in this notation:

select new {resx.Author, resx.PubDate};

As I mentioned before, this creates an anonymous type with an Author and PubDate property; in addition, you may see an issue because it wants them to be named as such:

select new { AUTH = resx.Author, PUBLISHED = resx.PubDate};

Now, the anonymous type has our new property names.

Published Monday, January 07, 2008 4:58 AM by bmains
Filed under:

Comments

# re: Basic LINQ Queries From a Collection

Sunday, January 06, 2008 11:44 PM by Mehfuz Hossain

That's Nice, you can also check out my LinqExtender project that will enable you to create any LINQ provider easily. :-). See if you like it, and let me know.

# re: Basic LINQ Queries From a Collection

Friday, February 29, 2008 9:30 PM by zxevil135

JzPlni r u crazzy? I told u! I can't read!

# re: Basic LINQ Queries From a Collection

Saturday, March 01, 2008 3:04 PM by zxevil136

bbcpJU r u crazzy? I told u! I can't read!

# re: Basic LINQ Queries From a Collection

Thursday, March 06, 2008 4:41 PM by zxevil134

GjMNFE r u crazzy? I told u! I can't read, man!

# re: Basic LINQ Queries From a Collection

Thursday, March 06, 2008 7:41 PM by zxevil141

HgDM7p r u crazzy? I told u! I can't read!

# re: Basic LINQ Queries From a Collection

Friday, March 07, 2008 1:27 PM by zxevil150

VxKtFX r u crazzy? I told u! I can't read!

# re: Basic LINQ Queries From a Collection

Friday, March 07, 2008 4:28 PM by zxevil151

CREDwq r u crazzy? I told u! I can't read!

# re: Basic LINQ Queries From a Collection

Friday, March 07, 2008 7:34 PM by zxevil152

hhHYHk r u crazzy? I told u! I can't read!

# re: Basic LINQ Queries From a Collection

Friday, March 07, 2008 11:00 PM by zxevil153

gq7Hs3 r u crazzy? I told u! I can't read!