Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# Linq包含运算符_C#_Asp.net_Linq_Contains - Fatal编程技术网

C# Linq包含运算符

C# Linq包含运算符,c#,asp.net,linq,contains,C#,Asp.net,Linq,Contains,这很好: var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select c.SpecID).Distinct().ToList(); var qq = (from s in db.tblSpecifications where q.Contains(s.id) select s); 但是,我现在需要在第一个查询中返回另一个字段: var q = (from c in db.t

这很好:

var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select c.SpecID).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Contains(s.id) select s);
但是,我现在需要在第一个查询中返回另一个字段:

var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select new { c.SpecID, c.FriendlyName }).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Contains(s.id) select s);
因此
q.contains
现在失败了,我需要它来处理
q
query
SpecID
字段。有人知道怎么做吗?

好吧,你可以试试:

换句话说,在使用
Contains
之前投影结果。我不知道SQL将是什么样子

顺便说一句,我个人写的是:

var qq = db.tblSpecifications
           .Where(s => q.Select(x => x.SpecID).Contains(s.id));
我只在查询表达式语法使事情更简单的地方使用它。我还强烈建议您在查询中使用多行,这确实有助于提高可读性。

这就是您想要的吗

var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select new { c.SpecID, c.FriendlyName }).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Any(c => c.SpecID == s.id) select s);
var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select new { c.SpecID, c.FriendlyName }).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Any(c => c.SpecID == s.id) select s);