English 中文(简体)
Linq is Null,不是Null 问题
原标题:Entity Framework Linq Is Null, Is Not Null Issue

我有一种方法 得到一个类别ID, 之后有两个默认为无效的可选字符串参数 。

我尝试使用其他问题中的一些类似答案,

我试图让Linq到EF查询工作如下:

如果任一可选参数都有值,则使用该值,否则使用 `Is Null' 。

如果两种可选参数都作为查询的一部分使用,或者仅根据eis 提供时使用,则使用这些参数。但如果未添加参数,请使用类别 ID。

db 中两个可选参数被标为无效 。

这是无效的代码:

          from c in dtx.Categories
          where c.CategoryId == CatId
         && (string.IsNullOrEmpty(param1) ? c.Param1 == null : c.Param1 == param1)
         && (string.IsNullOrEmpty(param2) ? c.Param2 == null : c.Param2 == Param2)
        select c

尝试二 :

          from c in dtx.Categories
          where c.CategoryId == CatId
          && (c.Param1 == null ? c.Param1 == null : c.Param1 == param1)
          && (c.Param2 == null ? c.Param2 == null : c.Param2 == param2)
          select c

没有丢弃错误, 但两个查询总是返回零结果, 除非两个参数都存在 。

One of the posts I tried: How can i query for null values in entity framework?

最佳回答

从我所看到的来看,问题似乎查询的条件没有正确写入。让我们用一个示例来检查会附加什么 :

数据 :

Id = 1, Param1 = null, Param2 = null
Id = 2, Param1 =  a    param2 = null
Id = 3, Param1 = null, Param2 =  b 
Id = 4, Param1 =  a    param2 =  c 

With the current query and the other solution proposed you will only get the Id 1. Your condition are saying : If the Param1 Is Null and c.Param1 (the stored value) Is null OR c.Param1 Is Equal to Param1 value.

您需要的是一个条件 : 如果 帕拉姆1 是 Null 或 c. Param1 等于 帕拉姆1 值 。

如果您使用此查询, 总是能得到结果 。

from c in dtx.Categories
where c.CategoryId == CatId
    && (string.IsNullOrEmpty(param1) || c.Param1 == param1)
    && (string.IsNullOrEmpty(param2) || c.Param2 == param2)
select c
问题回答

也许这次我读对了问题:

var p1 = string.IsNullOrEmpty(param1) ? null : param1;
var p2 = string.IsNullOrEmpty(param2) ? null : param2;

var query = dtx.Categories.Where(c => c.CategoryId == CatId);
if (p1 != null || p2 != null) {
    query = query.Where(c => c.Param1 == p1 && c.Param2 == p2);
}

您应该明确显示复选, 两者要么是 null , 要么是匹配的 null

((string.IsNullOrEmpty(param1) && c.Param1 == null) || (c.Param1 == param1))

EDIT: 刚刚测试而无关紧要的是 u check null 或 SQL 是否相同, 请按

from c in dtx.Categories 
    where c.CategoryId == CatId 
       && (c.Param1 == param1) 
       && (c.Param2 == Param2) 
select c 

问题是当你写作的时候

from ... where c.Param1 == null ...

LINQ 将它翻译为同一种 SQL 表达式:

SELECT ... FROM ... WHERE Param1 = null ...

但你需要这个:

SELECT ... FROM ... WHERE Param1 IS NULL ...

因此,正确的解决方案是 :

from c in dtx.Categories
where c.CategoryId == CatId && 
     (param1 == null ? !c.Param1.HasValue : c.Param1.Value == param1) && 
     (param2 == null ? !c.Param2.HasValue : c.Param2.Value == param2)
select c




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签