Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用导航属性和Where子句的Linq查询_C#_Entity Framework_Linq_Lambda_Linq To Entities - Fatal编程技术网

C# 使用导航属性和Where子句的Linq查询

C# 使用导航属性和Where子句的Linq查询,c#,entity-framework,linq,lambda,linq-to-entities,C#,Entity Framework,Linq,Lambda,Linq To Entities,我正在尝试使用导航属性组合linq查询。我正在从3个实体中选择属性: 储物柜 截面柱 合同 我需要符合以下所有条件的储物柜表中的所有行:储物柜类型ID=“308”,.OutOfOrder!=true,x.SectionColumn.SectionId==“52”) 下面的查询没有条件x.SectionColumn.SectionId==“52”,它可以正常工作并返回我所需要的内容,但节id为任意值的行会像我预期的那样返回 from l in Lockers.Where(x =>

我正在尝试使用导航属性组合linq查询。我正在从3个实体中选择属性:

  • 储物柜
  • 截面柱
  • 合同
我需要符合以下所有条件的储物柜表中的所有行:储物柜类型ID=“308”,.OutOfOrder!=true,x.SectionColumn.SectionId==“52”)

下面的查询没有条件x.SectionColumn.SectionId==“52”,它可以正常工作并返回我所需要的内容,但节id为任意值的行会像我预期的那样返回

    from l in Lockers.Where(x => x.LockerTypeId == "308" && x.OutOfOrder != 
    true).DefaultIfEmpty()
               select new 
                {
                   ColumnNumber = l.ColumnNumber,
                   LockerTypeId = l.LockerTypeId,
                   OutOfOrder = l.OutOfOrder,
                   Rented = l.Contracts.Select(x => x.Contract_ID < 0 ?                   
                     false : true).FirstOrDefault(),
                   Section = l.SectionColumn.SectionId
                   }

如果您能帮助我正确编写此查询,我将不胜感激。

首先,如果您坚持使用纯LINQ,您的代码可能会更直接一些。在这种情况下,您的代码应该如下所示

 var results = from l in Lockers
               where l.LockerTypeId == "308" && l.OutOfOrder != true && l.SectionColumn.SectionId == "52"
               select new 
               {
                    ColumnNumber = l.ColumnNumber,
                    LockerTypeId = l.LockerTypeId,
                    OutOfOrder = l.OutOfOrder,
                    Rented = l.Contracts.Select(x => x.Contract_ID < 0 ? false : true).FirstOrDefault(),
                    Section = l.SectionColumn.SectionId
               }

请编辑第二个代码示例。那里没有
SectionId
。Section=l.SectionColumn!=null?l.SectionColumn.SectionId?0:0如果SectionColumn在表架构中为allow null?在访问l.SectionColumn.SectionId之前,必须检查l.SectionColumn是否等于null,只要某些SectionColumn为null值,在访问l.SectionColumn.SectionId之前,此解决方案不可能是真的。您必须检查l.SectionColumn不等于null谢谢-我会尝试一下。@。不知道为什么lambda版本不起作用。谢谢你的建议。
 var results = from l in Lockers
               where l.LockerTypeId == "308" && l.OutOfOrder != true && l.SectionColumn.SectionId == "52"
               select new 
               {
                    ColumnNumber = l.ColumnNumber,
                    LockerTypeId = l.LockerTypeId,
                    OutOfOrder = l.OutOfOrder,
                    Rented = l.Contracts.Select(x => x.Contract_ID < 0 ? false : true).FirstOrDefault(),
                    Section = l.SectionColumn.SectionId
               }
 var results = from l in Lockers
               let sectionId = (l.SectionColumn != null) ? l.SectionColumn.SectionId : null
               where l.LockerTypeId == "308" && l.OutOfOrder != true && sectionId == "52"
               select new 
               {
                    ColumnNumber = l.ColumnNumber,
                    LockerTypeId = l.LockerTypeId,
                    OutOfOrder = l.OutOfOrder,
                    Rented = l.Contracts.Select(x => x.Contract_ID < 0 ? false : true).FirstOrDefault(),
                    Section = l.SectionColumn.SectionId
               }