【问题标题】:Converting Sql query with 2 "left Join" and multi conditions to entity framework query将具有 2 个“左联接”和多条件的 Sql 查询转换为实体框架查询
【发布时间】:2018-05-15 12:06:48
【问题描述】:

我试图转换这个 sql 查询

Select * 
from (
    select SenDa.*,Prod.ProductKHK,FCod.FailCodeDesc  
    from databa.dbo.SensorData as SenDa 
    left join Products as Prod on SenDa.ProductID = Prod.ProductID
    left join FailCodes as FCod on SenDa.FailCode = FCod.FailCode and (FCod.ProdLineID =0 or FCod.ProdLineID = FCod.ProdLineID)
) as SenDa

到 sql Linq 所以:

var SensDatJoinFail = (from SensDat in Jas_en.SenDatas
join Prod in Jas_en.Products on 
SensDat.ProductID equals Prod.ProductID
join FCod in Jas_en.FailCodes on SensDat.FailCode equals FCod.FailCode1
where FCod.ProdLineID == 0 || FCod.ProdLineID == FCod.ProdLineID

                               select new
                          {
                              Serial_No = SensDat.Serial_No,
                              OrderID = SensDat.OrderID,
                              Artikelnummer = Prod.ProductKHK,
                              StartProcTime = SensDat.StartProcTime,
                              EndProcTime = SensDat.EndProcTime,
                              Packaged = SensDat.Packaged,
                              Labeled = SensDat.Labeled,
                              Reworked = SensDat.Reworked,
                              LastStation = SensDat.LastStation,
                              FailCode = SensDat.FailCode,

                              FailCodeDesc = FCod.FailCodeDesc,

                          });

但我在行数中的查询结果有所不同

不知道,问题出在哪里?

【问题讨论】:

    标签: c# sql sql-server entity-framework linq-to-sql


    【解决方案1】:

    这是一个 EF 常见问题解答。不要在 LINQ to Entitites 中使用 JOINS。只需导航您的导航属性。比如:

       from SensDat in Jas_en.SenDatas
        select new
        {
          Serial_No = SensDat.Serial_No,
          OrderID = SensDat.OrderID,
          Artikelnummer = SensDat.Product.ProductKHK,
          StartProcTime = SensDat.StartProcTime,
          EndProcTime = SensDat.EndProcTime,
          Packaged = SensDat.Packaged,
          Labeled = SensDat.Labeled,
          Reworked = SensDat.Reworked,
          LastStation = SensDat.LastStation,
          FailCode = SensDat.FailCode,
          FailCodeDesc = SensDat.FailCode.FailCodeDesc,
        });
    

    【讨论】:

    • 谢谢 是的,我知道:但问题是我无法更改数据库结构并且表之间没有关系。因此我使用了join。
    • 您不需要数据库中的 FK 在您的 EF 模型中具有导航属性。
    【解决方案2】:

    您获得的行数减少了,因为您使用的是 inner 联接而不是 left 联接。试试这样的:

    var result= from table1 in dbo.Table1 
                from table2 in dbo.Table2
                               .Where(i=>i.Id == table1.Id)
                               .DefaultIfEmpty();
    

    或者你可以看看如何Perform left outer joins

        Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
        Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
        Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
        Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
    
        Pet barley = new Pet { Name = "Barley", Owner = terry };
        Pet boots = new Pet { Name = "Boots", Owner = terry };
        Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
        Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
        Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
    
        // Create two lists.
        List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
        List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };
    
        var query = from person in people
                    join pet in pets on person equals pet.Owner into gj
                    from subpet in gj.DefaultIfEmpty()
                    select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };
    

    【讨论】:

    • 这对你很有帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多