C# linq 查询其中 int ID 属于 List<int>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2098757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:29:14  来源:igfitidea点击:

linq query where int ID belongs to List<int>

c#linqlist

提问by Slabo

I'm having a problem with a linq query. I have used this similarly before but i cannot understand what could be wrong now.

我在使用 linq 查询时遇到问题。我以前也用过类似的方法,但我不明白现在可能出了什么问题。

Errors

错误

The best overloaded method match for 'System.Collections.Generic.List.Contains(int)' has some invalid arguments
Argument '1': cannot convert from 'int?' to 'int' ; refers to the where clause of rTestResults

“System.Collections.Generic.List.Contains(int)”的最佳重载方法匹配有一些无效参数
参数“1”:无法从“int”转换?'int'; 指的是 rTestResults 的 where 子句

Code:

代码:

List<int> rMTCPlates = (from rP in mDataContext.Plates
                        where rP.SOItem.SONumberID == aSONumber
                        select rP.ID).ToList();

var rTestResults = from rT in mDataContext.TestSamplesViews
                   where rMTCPlates.Contains(rT.PlateID)
                   select rT;  

Any idea whats going on?

知道发生了什么吗?

Any help appreciated,
Thanks

任何帮助表示赞赏,
谢谢

采纳答案by bendewey

You can use the null-coalescing operatorhere:

您可以在此处使用空合并运算符

var rTestResults = from rT in mDataContext.TestSamplesViews 
               where rMTCPlates.Contains(rT.PlateID ?? 0) 
               select rT; 

回答by Shimmy Weitzhandler

That's because the ID field is nullable, while the items in the collection are not.

这是因为 ID 字段可以为空,而集合中的项目则不能。

You probably forgot to mark it as not-null in the database so the designer, generated it this way.

您可能忘记在数据库中将其标记为非空,因此设计者以这种方式生成它。

Best thing mark in the DB is not-null and refresh data.

数据库中最好的标记是非空和刷新数据。

Besides, U didn't specify which LINQyou're using, assuming you linq the DB, I am not sure the Contains extension method works in Linq to Sql or Linq to Entities, you might get another NotSupportedException even after you fix the above.

此外,您没有指定您正在使用哪个LINQ,假设您 linq 数据库,我不确定包含扩展方法在 Linq to Sql 或 Linq to Entities 中是否有效,即使您修复了上述问题,您也可能会得到另一个 NotSupportedException。

Therefore, either use two queries, the first to access the DB and the second to retrieve you value from the loaded collection, or consider using 'Contains()' workaround using Linq to Entities?.

因此,要么使用两个查询,第一个访问数据库,第二个从加载的集合中检索您的值,或者考虑使用使用 Linq to Entities 的“Contains()”解决方法?.

And if you're using linq-to objects and you don't care about all the trash said above, just use the following:

如果您正在使用 linq-to 对象并且您不关心上面所说的所有垃圾,只需使用以下内容:

var rTestResults = from rT in mDataContext.TestSamplesViews  
                   where rT.PlateID.HasValue &&
                       rMTCPlates.Contains(rT.PlateID.Value)  
                   select rT.Value; 

回答by Dynami Le Savard

Since the exception in on System.Collections.Generic.List.Contains(int)I can guess that the mDataContext.TestSamplesViews.PlateIDis nullable.

由于 on 中的异常,System.Collections.Generic.List.Contains(int)我可以猜测mDataContext.TestSamplesViews.PlateID是可为空的。

If you just want an easyquick fix, and/or you cant change PlateIDso it isn't nullable anymore, you can check if it has a value first :

如果你只是想要一个简单的快速修复,和/或你不能改变PlateID所以它不再可以为空,你可以先检查它是否有一个值:

var rTestResults = from rT in mDataContext.TestSamplesViews
                   where (rT.PlateID.HasValue && rMTCPlates.Contains(rT.PlateID))
                   select rT;