使用此代码时(简化要求):
var rows1 = (from t1 in db.TABLE1
where (t1.COLUMN_A == 1)
select new { t1.COLUMN_B, t1.COLUMN_C });
var rows2 = (from t2 in db.TABLE2
where (rows1.Contains(t2.COLUMN_A))
select t2;
我收到以下错误:
方法的类型参数 “System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)'无法从中推断出来 用法。尝试指定类型 明确的论点。
我需要通过COLUMN_B过滤第一个结果,但我不知道如何。有没有办法过滤它?
答案 0 :(得分:9)
要使用Contains,您必须在IEnumerable<T>
中传递该类型的实例。匿名类型非常难。
相反,我会使用Any
扩展方法重载,它允许您指定比较lambda。例如
var rows2 = (from t2 in db.TABLE2
where (rows1.Any(x => x.COLUMN_B == t2.COLUMN_A))
select t2;
答案 1 :(得分:1)
尝试使用Any
var rows1 = (from t1 in db.TABLE1
where (t1.COLUMN_A == 1)
select new { t1.COLUMN_B, t1.COLUMN_C });
var rows2 = (from t2 in db.TABLE2
where (rows1.Any( r => r.COLUMN_B == t2.COLUMN_A))
select t2;
答案 2 :(得分:0)
这有效吗?
var rows1 = (from t1 in db.TABLE1
where (t1.COLUMN_A == 1)
select new { t1.COLUMN_B, t1.COLUMN_C }).ToList();
var rows2 = (from t2 in db.TABLE2
where (rows1.Contains(t2.COLUMN_A))
select t2;