LINQ中的where语句来自sql

时间:2017-11-23 09:58:11

标签: linq

我正在尝试将SQL语句转换为LINQ。

我把它作为SQL:

SELECT mutDetail.*
FROM hallo.Mutatie as mut
JOIN hallo.MutatieDetail AS mutDetail ON mut.MutatieID =  mutDetail.fkMutatieID
JOIN tblMedewerker AS med ON med.MedewerkerID = mut.fkMedewerkerID
JOIN tblKlant AS klant ON klant.KlantID = med.fkKlantID
WHERE mutDetail.BronCode = 'Personeelsnummer' 
  AND klant.KlantNummer = '78555522'

这是我的LINQ声明:

public IQueryable<Mutatie> GetMutatiesInstroom()
{
    var mutaties = (from m in context.Mutatie
                    join mutDetail in context.MutatieDetail on m.MutatieID equals mutDetail.fkMutatieID
                    join med in context.tblMedewerker on m.fkMedewerkerID equals med.MedewerkerID
                    join klant in context.tblKlant on med.fkKlantID equals klant.KlantID                                                        
                    select m);

    return mutaties;
}

但是如何在LINQ中编写where语句?

谢谢

3 个答案:

答案 0 :(得分:2)

我不知道问题是什么,但是不简单吗?在select m之前:

where m.BronCode == "Personeelsnummer" && klant.KlantNummer == "8700"

按要求完成查询:

var mutaties = from m in context.Mutatie
               join mutDetail in context.MutatieDetail on m.MutatieID equals mutDetail.fkMutatieID
               join med in context.tblMedewerker on m.fkMedewerkerID equals med.MedewerkerID
               join klant in context.tblKlant on med.fkKlantID equals klant.KlantID        
               where m.BronCode == "Personeelsnummer" && klant.KlantNummer == "8700"                                             
               select m;

答案 1 :(得分:1)

它应该在这里:

public IQueryable<Mutatie> GetMutatiesInstroom()
{
    var mutaties = (from m in context.Mutatie
                    join mutDetail in context.MutatieDetail on m.MutatieID equals mutDetail.fkMutatieID
                    join med in context.tblMedewerker on m.fkMedewerkerID equals med.MedewerkerID
                    join klant in context.tblKlant on med.fkKlantID equals klant.KlantID   
                    where (mutDetail.BronCode == "Personeelsnummer" && 
                           klant.KlantNummer == "8700")                                
                    select m);

    return mutaties;
}

答案 2 :(得分:0)

以下是microsofts文档中有关如何在Linq语句中添加过滤器的示例。

var queryLondonCustomers = from cust in customers
                       where cust.City == "London"
                       select cust;
  

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/basic-linq-query-operations

在Filtering

部分下方