101 Linq Samples

16 02 2010

There is a while that I have this link in my “Linq bookmarks collection” and I think it will be helpfull for beginner and anyone who has forgotten a scrap of Linq syntax or has failed to build a query…

http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx





Linq to SQL Boolean operation convertion

5 03 2009

Another limit when using Linq To SQL is the SQL translation of boolean operation.
For exemple, when writting:

from s in sTable
select new{
BoolValue=BoolValue1&&BoolValue2
}

the corresponding SQL interpretation will be like that:

select BoolValue=case when (BoolValue1 and BoolValue2) then 1
when !(BoolValue1 and BoolValue2) then 0
else NULL end
from sTable

However, the wished result is 1 or 0, not NULL .
Be aware of that when processing boolean operation…





Linq To SQL : LinqContext per Unit of Work

11 11 2008

To implement a Linq To SQL Oriented solution, there are many ways to construct your application. But only one interested me : the Context per Unit of Work Architecture.

When using Linq To SQL, you need to instanciate a Context which contains your entities tables and allow you to apply query methods. But when your application is construct with many layers, you only can pass Context through methods to keep the same context during your business operations. It is the same problem when a transaction is required(managed by Linq To SQL Context though optimistic transaction).

Read the rest of this entry »





Linq To Entity/Entity Framework with SP1

22 10 2008

When the Linq word begin to spread to the developper’s world, I immediatly inform me about what is it and what can I do with “that”. After trying Linq To Object, Linq To XML, Linq To SQL and Linq To Entity, I was convinced that it was the right path to follow. But for Data management, I was preferring Linq To SQL because Linq To Entity utilisation was boring(Stored Procedure to write).
After SP1, Linq To Entity was not the same. So I studied it more deeper… And some good things falls.

Read the rest of this entry »





Linq To SQL Generic Controller

20 10 2008

With the power of Linq language, it’s easy to use Linq To SQL to build 95% of the data access interrogation.

But you will notify that 2/3 of your data access methods is always the same, as GetById, GetAll, Insert, DeleteById…

So Microsoft staff has written a class named GenericController that allow you to centralize all standard methods in one class. You only need to inherit from it and a good part of your job is done.

http://code.msdn.microsoft.com/multitierlinqtosql

If you look more deeper in this pack, you will find some other classes written to instanciate one LinqContext by Query or a Linq To SQL Debugger(see also my ticket on DebugWriter).





View Linq SQL Queries in VS Debug mode

20 10 2008

When you begin to write complex Linq To SQL Queries with many IQueryable associations, it must be difficult to catch all the SQL generated parts by Linq To SQL. That’s why, after searching on the web some information, I have found this little class that can be usefull : DebugWriter.

Read the rest of this entry »





Linq Synchronizer

7 10 2008

The first time I was using Linq To SQL, I was frustrated by the fact VS2008 did not integrated the more simple fonctionnality called : “Synchronize DBML Shema with Database”. Just removing my classes from DBML Shema and perform a Drag&Drop from Server Explorer to schema was to much for me ^^.

So I wrote a simple VS 2008 plug-in which allow with 2 clics a synchronization between my database and My DBML Schema.

Read the rest of this entry »





Linq To SQL : How to order with a string typed expression ?

23 09 2008

After using Linq To SQL queries, I was quickly confronted to sort my data with a string expression provided, for example, by a gridview when a user require it. At this moment, I understood that the strongly typed system used with Linq cannot help me for this task… Understanding the fact that allowing a string expression representing an object property is contrary to Linq purpose, I began to search with our best friend “Google” a soluce to resolve quickly my problem. But the best that I can obtain was a “switch case solution” which, according to the string property name, build the linq query sort filter with the associated object property. Immediatly, I guess my switch case code with 8-10 columns… No…

So, I started to write an extended method “OrderBy” allowing to take a string sort expression. Here the result.

Read the rest of this entry »





Linq Overview

23 09 2008

During my migration from .NET 2.0 to .NET 3.0/3.5, my first tought was : ‘With .NET 3.5, I can used Linq To SQL and stop to bore me with all the code generation tools”. I don’t want to crush the glory of tools like Raptier or Code Smith, but an ORM produce by Microsoft can be better in generation time(class generation and integration) and simplicity. It was true(for common uses). But better than that. Linq offers many others functionalities(strongly typed, composite queries, relashionship between objects and many more). And I began to forget all about others problems when the Graal was not here…

After a month of research, I have started to write a document on my Linq feedback and I share it today 🙂

linq