While attending the recent MSDN Roadshow, we couldn’t help but notice the strong interest in the heretofore “missing LINQ,” which made its debut in .NET 3.0 and can be seen creeping its way into application source code.
You may ask “What is LINQ?” and “Is it worth the investment?” Below we address these with some examples that can bring immediate results.
LINQ stands for Language Integrated Query and provides a way of performing queries against data sources as a part of the C# language. Besides ease of use, variables are strongly typed (see: http://en.wikipedia.org/wiki/Strongly_typed ) and validated at compile time, which improves reliability and programmer productivity.
Though LINQ offers a list of robust features, it takes very little effort to benefit from the core concepts because of the similarity between LINQ and SQL queries.
Simple Example
A SQL query like:
SELECT saleRevenue FROM Sales WHERE SalesRepId = 45
is written in LINQ C# as:
var salesDollars = from S in Sales
where S.SalesRepId == 45
select S.SalesRevenue;
double totalSales = 0.0;
foreach (var selectedSale in salesDollars)
totalSales += selectedSale;
Notice the query is reordered to create the LINQ statement. Strong typing isn’t obvious in the above snippet. I left off the class definition and initialization, which looks like this:
class Sales
{
private double _SaleRevenue;
private int _SalesRepId;
private DateTime _DateClosed;
public double SalesRevenue
{
get { return _SaleRevenue; }
set { _SaleRevenue = value; }
}
public int SalesRepId
{
get { return _SalesRepId; }
set { _SalesRepId = value; }
}
public DateTime DateClosed
{
get { return _DateClosed; }
set { _DateClosed = value; }
}
}
The class defines a strongly-typed record. Let’s build sample data:
var Sales = new List<Sales> {
new Sales {
SalesRevenue = 8000.40,
SalesRepId = 45,
DateClosed = DateTime.Parse("3/27/2003") },
new Sales {
SalesRevenue = 4267.10,
SalesRepId = 45,
DateClosed = DateTime.Parse("4/1/2003") },
new Sales {
SalesRevenue = 876.99,
SalesRepId = 10,
DateClosed = DateTime.Parse("12/15/2002") }
};
Now the LINQ query can select from the var Sales in the previous example and totals the first two entries in the list.
Complex Results
After reviewing this, you may ask: “What if I need more than just one item returned in the query?”
This is the nice thing about var. It holds complex types as in the following where we want to compute the commission in addition to the sale:
var salesResult = from S in Sales
where S.SalesRepId == 45
select new
{
S.SalesRevenue,
Commission = S.SalesRevenue * 0.15
};
The anonymous type var salesResult now contains both SalesRevenue and Commission properties that can be accessed:
double totalSales = 0.0;
double totalCommission = 0.0;
foreach (var selectedResult in salesResult)
{
totalSales += selectedResult.SalesRevenue;
totalCommission += selectedResult.Commission;
}
More Complex Expressions
Queries used in real applications are more complicated than the above examples, but can be expanded in the same way we are used to seeing in SQL.
The following shows both Joining and Ordering.
var X = from S in Sales
join c in Customers on o.CustomerID equals S.CustomerID
where S.SalesRevenue > 1000.00
orderby S.SalesRepId
select new
{
S.SalesRepId,
c.CustomerID,
S.SalesRevenue,
Commission = S.SalesRevenue * 0.15
};
LINQ also allows multiple Froms and Grouping as well.
Summary
We have shown LINQ here, but .NET 3.0 also supports DLINQ to query directly against a database.
LINQ and DLINQ bring a lot of power to the C# developer with this new declarative format and we hope you can see it takes very little effort to benefit from it.
Your developers can realize excellent gains with virtually no pain.
To learn more about LINQ, visit the Microsoft library at http://msdn.microsoft.com/en-us/library/bb308959.aspxor ask your KMA representative to arrange a conversation with us.
Comments