日期:2014-05-20  浏览次数:21035 次

一个蛋疼的lambda
C# code

var query=from s in db.SubArea
......此处省略
select new{
....//此处省略
Zhan = db.Customer.Where(c => (db.graveTemp.Where(g => g.subarea == s.Id).Select(g => g.gvid).Contains(c.FgrvId)))
};
//提示错误错误    “System.Linq.IQueryable<int>”不包含“Contains”的定义,并且最佳扩展方法重载 “System.Linq.ParallelEnumerable.Contains<TSource>(System.Linq.ParallelQuery<TSource>, TSource)”的某些参数无效    F:\projects\TombPrj\TombDAL\GraveService.cs    346    60    TombDAL



缩水版表结构如下
SubArea:
Id SubName
graveTemp:
Id FsubAreaId
Customer
Id FgraveTempId
想使Zhan的值为 Customer中出现FgraveTempId为 graveTemp 中FsubAreaId=s.Id(即SubArea中的id)的Id 
写成sql为
SELECT COUNT(*) FROM CUSTOMER WHERE FgraveTempId in (select id from graveTemp where FsubAreaid=s.Id)

我貌似描述明白了 不知道你们看着凌乱不。

------解决方案--------------------
try..

C# code
var query=(from s in db.SubArea
           join g in db.graveTemp on g.subarea equals s.Id select g.gvid)
          .Count(gid => db.Customer.Any(c=>c.FgrvId==gid));

------解决方案--------------------
var query=from s in db.SubArea
......此处省略
select new{
....//此处省略
Zhan = from c in db.Customer
let subIds=db.graveTemp.Where(g => g.subarea == s.Id).Select(g => g.gvid)
where subIds.Contains(c.FgrvId)
select c;
};