DLinq: Dlinq is the new ADO.NET for the Microsoft.NET Platform. DLinq allows you to execute the T-SQL queries using any .NET compatible programming language.
Creating Mapping B/W Class and Database Tables:
First thing you need to do is to create a class that maps to the database table.
[Table (Name="Orders")]
class Order
{
[Column (DbType="int not null",Id=true)]
public int OrderID;
[Column (DbType="nchar(50)")]
public string CustomerID;
[Column (DbType="nvarchar(60)")]
public string ShipAddress;
[Column (DbType="DateTime")]
public DateTime OrderDate;
}
This is equavalent to the following T-SQL Statements:
CREATE TABLe Orders
(
OrderID int identity(1,1) PRIMARY KEY,
Customer nchar(50),
ShipAddress nvarchar(60),
OrderDate DateTime
)
Retrieving data from the Table:
DataContext context = new DataContext(
"Server=localhost;Database=Northwind;Trusted_Connection=true");
Table orders = context.GetTable();
Table customer = context.GetTable();
var query = from c in customer, o in orders
where c.CustomerID == o.CustomerID
select new { c.ContactName,o.OrderID };
foreach(var item in query)
{
Console.WriteLine(item.ContactName);
Console.WriteLine(item.OrderID);
}
If you use the above query it will return you duplicates. In order to remove the duplicates you can do the following:
IEnumerable expr = from c in customers,o in orders
where c.CustomerID == o.CustomerID
select c;
expr = expr.Distinct();
foreach(Customer c in expr)
{
Console.WriteLine(c.ContactName);
}
Counting the number of records in the Database Table:
// Count the number of records in the Database Table
Table customers = context.GetTable();
IEnumerable cust = from c in customers
select c;
int count = cust.Count();
Console.WriteLine(count);
You can also find the number of Customers based on a certain condition:
// Count the number of records in the Database Table
Table customers = context.GetTable();
IEnumerable cust = from c in customers
select c;
int count = cust.Count(s => s.Country == "Mexico");
Console.WriteLine("Number of Customers in Mexico: " + count);
This was just the Tip of DLinq. Download it and do the geeky stuff with Dlinq.