This is first time I am writing a BLOG post with an error messageJ. This error message you will experience while writing code in Orcas Jan CTP. There is no LINQ Windows or LINQ Console Application template available. So no references/namespace directives will be added automatically. If you have installed Orcas Jan CTP and are trying to create LINQ Apps there and follow the Charlie’s BLOG, you need to manually add couple of dlls from \Windows\Microsoft.NET\Framework\v3.5.XXXXX\ (I have version 3.5.11209 ie. Microsoft .NET Framework 3.5 (GreenBits)). Dlls are System.Core.dll, System.Data.Linq.dll, System.Xml.Linq.dll and add a reference “System.Query”. But still that will throw you and error for the simple code
int[] nums = {1,2,3,4,5,6,7};
var q = from n in nums
where n>5
select n;
There where will throw you and error with the following message
‘System.Data.TypedTableBaseExtensions.Where<T>(System.Data.TypedTableBase<T>, System.Query.Func<T,bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
Simple solution is that you need to add a line at the top of your page using System.Query;
Hope this will solve your problem.
Namoskar
There are some changes happened with LINQ Project with the January CTP. The namespace names like System.Xml.XLinq or System.Query are no more there. Namespaces got changed to System.Data.Linq and System.Xml.Linq.
Other than namespace and api changes lot of .proj file and C# syntax changes have happened.
For detailed information visit Charlie’s BLOG
Today I would like to discuss on the DLinq the natural roadmap to ADO.NET vNext. Little walkthrough, I am going to explore here which I enjoyed a lot during my hands on.
Create LINQ Console application from your Visual Studio 2005.
Go to LINQ installation folder e.g., C:\Program Files\LINQ Preview\Bin. There you will find one exe with the name SqlMetal. Type /? to get the help information on all possible options. I am assuming that you have SQL Server 2000 Northwind database installed in your application.
Open Visual Studio 2005 Command prompt and type
/server:myServer\myDBInstance /database:Northwind /code:MyNorthwind.cs /language:csharp /namespace:MyProject /pluralize
This will create the MyNorthwind.cs code file in the folder from where executing. Next step would be to add that file to your LINQ Console application. Now your business layer is ready within even less that few seconds. Amazing!!!! “Thanks LINQ, thank you very much”, so you start watching your best TV show or read books go out because the SqlMetal has written 1200 lines of code for you. How much more luxury you want from life? It is beyond expectations.
Now inside your code under static void Main(string[] args)
Wish 0:
Let us have a little background on ObjectDumper.dll which comes with LINQ Project installation. This returns all IEnumerable<T> in fairly easy way. You do not need a foreach statement to iterate through the collection to print the output. And if you use Anonymous type, displaying the output is as easy as Printf in C days. This dll is being created some awesome power of Reflection. The installation folder contains the sample which has got the source code for this static class. Moreover you can easily decide the depth you iterate through. My demonstration will tell you how.
Wish 0+1:
The common task for all wish list is that you have to tell your business logic which database this will connect.
Northwind n = new Northwind
(@"Data Source=myServer\SQL2000;Initial Catalog=Northwind;Integrated Security=True");
//Get the customers
Table<Customer> customers = n.GetTable<Customer>();
Wish 1:
As you know SQL Server 2000 Northwind database has a table named Customer. Get all the customer first
var t = from c in customers
select c.CompanyName;
foreach(var t1 in t)
{
Console.WriteLine(t1);
}
This will pullout all the Company name field from customers table.
Wish 2:
Now you want to implement some filter there.
where c.City == "London"
Wish 3:
Get two fields from there. Create a class and the property of each field, then initialize that class. Be little smart and use Anonymous types. Where CRL will create the class at runtime and will return it as typed collection (generics). This ensures the typed safety and the simplicity in terms of coding. If you have to declare class and related properties every time you want to get different set of results is not a very feasible solution. Friends we are here.
//Anonymous
select new{c.CompanyName, c.ContactName};
//using foreach
//Output: {CompanyName=Around the Horn, ContactName=Thomas Hardy}
//Output: Around the Horn : Thomas Hardy
Console.WriteLine(t1.CompanyName + " : " + t1.ContactName);
//using ObjectDumper
//Output: CompanyName=Around the Horn ContactName=Thomas Hardy
ObjectDumper.Write(t);
Wish 4:
Let us join between customer and their orders.
//Join between Cutomers and Orders
from o in c.Orders
select new {c.CompanyName, o.OrderDate};
//Output:
/*
CompanyName=Around the Horn OrderDate=11/15/1996
CompanyName=Around the Horn OrderDate=12/16/1996
CompanyName=Around the Horn OrderDate=2/21/1997
CompanyName=Around the Horn OrderDate=6/4/1997
*/
Wish 5:
Now apply filer in orders too.
select new {
c.CompanyName,
Orders =
where o.ShipCity == "London"
select new {o.OrderDate, o.ShipCity}
};
//CompanyName=Around the Horn Orders=...
//CompanyName=B's Beverages Orders=...
Since this has one level deep iteration for orders. We can now use the power of ObjectDumper to easily get the results will small modification.
ObjectDumper.Write(t,1);
// Orders: OrderDate=8/26/1996 ShipCity=London
// Orders: OrderDate=3/11/1997 ShipCity=London
// Orders: OrderDate=3/24/1997 ShipCity=London
// Orders: OrderDate=5/15/1997 ShipCity=London
// Orders: OrderDate=5/16/1997 ShipCity=London
// Orders: OrderDate=6/24/1997 ShipCity=London
// Orders: OrderDate=7/15/1997 ShipCity=London
// Orders: OrderDate=3/11/1998 ShipCity=London
// Orders: OrderDate=3/13/1998 ShipCity=London
// Orders: OrderDate=4/14/1998 ShipCity=London
Wish 6:
Now last but not the least let us involve OrderDetails table to get the price of each order.
from od in o.OrderDetails
select new {o.OrderDate,
TotalPrice =
(((float)od.Quantity * (float)od.UnitPrice)
- (float)od.Discount)}
/*Output:
CompanyName=Around the Horn Orders=...
Orders: OrderDate=11/15/1996 TotalPrice=90
Orders: OrderDate=11/15/1996 TotalPrice=390
Orders: OrderDate=12/16/1996 TotalPrice=96
Orders: OrderDate=12/16/1996 TotalPrice=195
Orders: OrderDate=12/16/1996 TotalPrice=608
Orders: OrderDate=2/21/1997 TotalPrice=152.9
Orders: OrderDate=2/21/1997 TotalPrice=299.9
Orders: OrderDate=6/4/1997 TotalPrice=237.5
Orders: OrderDate=6/4/1997 TotalPrice=1060
Orders: OrderDate=6/4/1997 TotalPrice=210
Orders: OrderDate=6/4/1997 TotalPrice=590.4
Orders: OrderDate=6/4/1997 TotalPrice=45
Orders: OrderDate=10/16/1997 TotalPrice=504
Orders: OrderDate=10/16/1997 TotalPrice=780
Orders: OrderDate=10/16/1997 TotalPrice=419.85
Orders: OrderDate=11/14/1997 TotalPrice=284.8
Orders: OrderDate=11/17/1997 TotalPrice=335.95
Ohh!!! It is weekend. I crossed a day (lost another day J). Happy weekend.
Namoskar!!!
If you are Office 2007 user, I bet if you download and use this add-in your going to enjoy the power.
So go ahead and download http://www.microsoft.com/downloads/details.aspx?FamilyID=4d951911-3e7e-4ae6-b059-a2e79ed87041&DisplayLang=en
Must read Gianpaolo’s BLOG at My 2006 SaaS Posts Hit Parade. In case if you feel that you need more number of hits to reach there here it is J
#1 SaaS Simple Maturity Modelhttp://blogs.msdn.com/gianpaolo/archive/2006/03/06/544354.aspx
#2 Understanding SaaS Architecture Powerpoint Presentationhttp://blogs.msdn.com/gianpaolo/archive/2006/10/03/Understanding-SaaS-Architecture-Powerpoint-Presentation.aspx
#3 SaaS Architectureshttp://blogs.msdn.com/gianpaolo/archive/2006/10/01/SaaS-Architectures.aspx
#4 SaaS ISV Architecture Questionnaire version 1.0http://blogs.msdn.com/gianpaolo/archive/2006/12/09/saas-isv-architecture-questionnaire-version-1-0.aspx
#5 TechEd US SaaS Presentation Available Onlinehttp://blogs.msdn.com/gianpaolo/archive/2006/10/07/TechEd-US-SaaS-Presentation-Available-Online.aspx
#6 SaaS in the enterprise (or the need of the external service bus?!)http://blogs.msdn.com/gianpaolo/archive/2006/08/19/707876.aspx
#7 Introduction to SaaS Architecture Podcasthttp://blogs.msdn.com/gianpaolo/archive/2006/11/18/introduction-to-saas-architecture-podcast.aspx
#8 Mr. and Mrs. CIO SaaS will not make your life simpler (but it is not necessarily a bad thing)http://blogs.msdn.com/gianpaolo/archive/2006/11/27/mr-and-mrs-cio-saas-will-not-make-your-life-simpler-but-it-is-not-necessarily-a-bad-thing.aspx
#9 SaaS and Biologyhttp://blogs.msdn.com/gianpaolo/archive/2006/11/09/saas-and-biology.aspx
#10 SaaS and IT Governancehttp://blogs.msdn.com/gianpaolo/archive/2006/08/20/709844.aspx
#11 The “multi-tenant” emperor has not clotheshttp://blogs.msdn.com/gianpaolo/archive/2006/08/30/731292.aspx
#12 SaaS: An Enterprise Perspectivehttp://blogs.msdn.com/gianpaolo/archive/2006/10/30/saas-an-enterprise-perspective.aspx
System.Xml has given the communication power to dot net and XLinq has refined that. It was very nice experience we have explored how to read XML using . NET. Now we are more excited to see how easily we can get the required value with less code (which was one of the philosophies behind the LINQ Project). Let us have an example on that.
Assume this is your XML
<?xml version="1.0" encoding="utf-8" ?>
<contacts>
<contact>
<name age="28">Wriju</name>
<email>wriju@abc.com</email>
</contact>
<name age="25">Tupur</name>
<email>tups@abc.com</email>
</contacts>
To read the Name and Email element in traditional .NET way you need to write this code (may be not standard but more or less solves the purpose)
string strOutput = "";
XmlTextReader reader = new XmlTextReader(@"..\..\Contacts.xml");
strOutput += "\r\n";
while(reader.Read())
switch(reader.NodeType)
case XmlNodeType.Element:
if(reader.Name=="name")
strOutput += "\r\nname:";
if(reader.Name=="email")
strOutput += "\r\nemail:";
break;
case XmlNodeType.Text:
strOutput += reader.Value;
The same can be achieved through XLinq (using the namespace System.Xml.XLinq)
var doc = XDocument.Load(@"..\..\Contacts.xml");
var items = doc.Element("contacts").Elements("contact");
foreach(var item in items)
strOutput += "\r\n" +
item.Element("name").Name + ":" + item.Element("name").Value;
item.Element("email").Name + ":" + item.Element("email").Value;
Less code and more powerful, I am now exploring the XLinq query and it awesome. I will write on that in my next blog.