2010-06-16 6 views
1

Comment puis-je réécrire le dessous de requête SQL à son LINQ équivalent 2 expression SQL (à la fois en C# et VB.NET)Conversion de requêtes SQL pour LINQ 2 expression SQL

SELECT t1.itemnmbr, t1.locncode,t1.bin,t2.Total 
     FROM IV00200 t1 (NOLOCK) 
        INNER JOIN 
       IV00112 t2 (NOLOCK) 
       ON t1.itemnmbr = t2.itemnmbr 
       AND t1.bin = t2.bin 
       AND t1.bin = 'MU7I336A80' 
+0

Avez-réponse votre question? –

Répondre

3

EDIT: J'ai remarqué que vous avez demandé à la fois C# et VB.Net, j'ai donc ajouté un exemple VB.

C#

using (var txn = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions 
    { 
     IsolationLevel = IsolationLevel.ReadUncommitted 
    } 
)) 
{ 
    var results = from t1 in db.IV00200 
        join t2 in db.IV00112 
        on new { t1.itemnmbr, t1.bin } 
        equals new { t2.itemnmbr, t2.bin } 
        where t1.bin == 'MU7I336A80' 
        select new {t1.itemnmbr, t1.locncode, t1.bin, t2.Total}; 

    // Do something with your results 

} 

VB.Net

Dim results 
Dim transOptions = New TransactionOptions 
transOptions.IsolationLevel = IsolationLevel.ReadUncommitted 

Using txn As New TransactionScope(TransactionScopeOption.Required, transOptions) 
    results = From t1 In db.IV00200 _ 
       Join t2 In db.IV00112 _ 
       On New With {.itemnmbr = t1.itemnmbr, .bin = t1.bin} _ 
       Equals New With {.itemnmbr = t2.itemnmbr, .bin = t2.bin} _ 
       Where t1.bin = "MU7I336A80" _ 
       Select New With {.itemnmbr = t1.itemnmbr, .locncode = t1.locncode, .bin = t1.bin, .Total = t2.Total} 

    ' Do something with your results 

End Using 

Vous devrez ajouter System.Transactions aux références de votre projet. Le bloc TransactionScope recrée les conditions nolock dans votre requête d'origine.

[TransactionScope/nolock Source: http://madprops.org/blog/linq-to-sql-and-nolock-hints/]