1

我有一个遇到问题的实体查询的 linq。

var query = (from q in dc.Table1
                where (from a in dc.Table2 select a.TypeID).Contains(q.TypeID)
                select q);

此查询在 Linq to SQL 中有效。它应该产生一个这样的 sql 查询:

select * from Table1 where TypeID in (select TypeID from Table2)

抛出的错误表明 Linq to Entities 是“无法创建类型为“Table2”的常量值

我可以执行以下操作:

var typelist = (from q in dc.Table2 select q.TypeID).ToList();

var query = (from q in dc.Table1
                where typelist.Contains(q.TypeID)
                select q);

但这会产生 2 个 sql 查询而不是 1 个:

select distinct TypeID from Table2;
select * from Table1 where TypeID in (1,2,3,4,5,6..... etc......);

有任何想法吗

4

1 回答 1

2

我尝试了这个查询并且它有效:

var result = (from appgroup in test.AppGroupThemes
                      where (from t in test.Themes
                             select t.Id
                            ).Contains(appgroup.ThemeId)
                      select appgroup).ToList();
        Console.WriteLine(result.Count);
于 2013-05-22T02:21:36.407 回答