解决 LinqToSQls“不支持使用本地集合的查询"例外 [英] Working around LinqToSQls "queries with local collections are not supported" exception

查看:24
本文介绍了解决 LinqToSQls“不支持使用本地集合的查询"例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图返回一组人,其 ID 包含在本地创建的 id 集合(IQueryable)中

So, I'm trying to return a collection of People whose ID is contained within a locally created collection of ids ( IQueryable)

当我指定本地创建的集合"时,我的意思是 Ids 集合并非来自 LinqToSql 查询,而是以编程方式创建的(基于用户输入).我的查询如下所示:

When I specify "locally created collection", I mean that the Ids collection hasnt come from a LinqToSql query and has been programatically created (based upon user input). My query looks like this:

var qry = from p in DBContext.People
                  where Ids.Contains(p.ID)
                  select p.ID;

这会导致以下异常...

This causes the following exception...

不支持使用本地集合的查询"

"queries with local collections are not supported"

如何找到所有具有包含在我本地创建的 ID 集合中的 ID 的人?

How can I find all the People with an id that is contained within my locally created Ids collection?

是否可以使用 LinqToSql?

Is it possible using LinqToSql?

推荐答案

如果 Ids 是 List、数组或类似的,L2S 将转换为 contains.

If Ids is a List, array or similar, L2S will translate into a contains.

如果 Ids 是一个 IQueryable,只需在查询中使用它之前将其转换为列表.例如:

If Ids is a IQueryable, just turn it into a list before using it in the query. E.g.:

List<int> listOfIDs = IDs.ToList();  
var query =  
from st in dc.SomeTable  
where listOfIDs.Contains(st.ID)
select .....

这篇关于解决 LinqToSQls“不支持使用本地集合的查询"例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆