'包含()'使用Linq到实体的解决方法? [英] 'Contains()' workaround using Linq to Entities?
本文介绍了'包含()'使用Linq到实体的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Silverlight ADO.Net数据服务客户端api(因此Linq To Entities)创建一个使用where子句中的ids列表的查询。有没有人知道解决方法包含不受支持?
我想做这样的事情:
列表与LT;长> txnIds = new List< long?>();
//填充列表
var q = from t in svc.OpenTransaction
其中txnIds.Contains(t.OpenTransactionId)
select t;
尝试这个:
var q = from t in svc.OpenTransaction
其中txnIds.Any< long>(tt => tt == t.OpenTransactionId)
选择t;
但是得到了方法Any'不支持。
解决方案
更新:EF≥4支持 直接包含
(Checkout 任何
),所以你不需要任何解决方法。
public static IQueryable< TEntity> WhereIn< TEntity,TValue>
(
这个ObjectQuery< TEntity>查询,
表达式&FunC< TEntity,TValue>>选择器
IEnumerable< TValue>集合
)
{
if(selector == null)throw new ArgumentNullException(selector);
if(collection == null)throw new ArgumentNullException(collection);
if(!collection.Any())
return query.Where(t => false);
ParameterExpression p = selector.Parameters.Single();
IEnumerable< Expression> equals = collection.Select(value =>
(Expression)Expression.Equal(selector.Body,
Expression.Constant(value,typeof(TValue))));
表达式body = equals.Aggregate((accumulate,equal)=>
Expression.Or(accumulate,equal));
return query.Where(Expression.Lambda< Func< TEntity,bool>>(body,p));
}
//可选 - 允许静态集合:
public static IQueryable< TEntity> WhereIn< TEntity,TValue>
(
这个ObjectQuery< TEntity>查询,
表达式< Func< TEntity,TValue>>选择器,
params TValue [] collection
)
{
return WhereIn(query,selector,(IEnumerable< TValue>)集合);
}
使用:
< p $ p>
public static void Main()
{
using(MyObjectContext context = new MyObjectContext())
{
//使用方法1 - 作为集合提供的集合
var contacts1 =
context.Contacts.WhereIn(c => c.Name,GetContactNames());
//使用方法2 - 静态提供的集合
var contacts2 = context.Contacts.WhereIn(c => c.Name,
Contact1,
Contact2,
Contact3,
Contact4
);
}
}
I'm trying to create a query which uses a list of ids in the where clause, using the Silverlight ADO.Net Data Services client api (and therefore Linq To Entities). Does anyone know of a workaround to Contains not being supported?
I want to do something like this:
List<long?> txnIds = new List<long?>();
// Fill list
var q = from t in svc.OpenTransaction
where txnIds.Contains(t.OpenTransactionId)
select t;
Tried this:
var q = from t in svc.OpenTransaction
where txnIds.Any<long>(tt => tt == t.OpenTransactionId)
select t;
But got "The method 'Any' is not supported".
解决方案
Update: EF ≥ 4 supports Contains
directly (Checkout Any
), so you don't need any workaround.
public static IQueryable<TEntity> WhereIn<TEntity, TValue>
(
this ObjectQuery<TEntity> query,
Expression<Func<TEntity, TValue>> selector,
IEnumerable<TValue> collection
)
{
if (selector == null) throw new ArgumentNullException("selector");
if (collection == null) throw new ArgumentNullException("collection");
if (!collection.Any())
return query.Where(t => false);
ParameterExpression p = selector.Parameters.Single();
IEnumerable<Expression> equals = collection.Select(value =>
(Expression)Expression.Equal(selector.Body,
Expression.Constant(value, typeof(TValue))));
Expression body = equals.Aggregate((accumulate, equal) =>
Expression.Or(accumulate, equal));
return query.Where(Expression.Lambda<Func<TEntity, bool>>(body, p));
}
//Optional - to allow static collection:
public static IQueryable<TEntity> WhereIn<TEntity, TValue>
(
this ObjectQuery<TEntity> query,
Expression<Func<TEntity, TValue>> selector,
params TValue[] collection
)
{
return WhereIn(query, selector, (IEnumerable<TValue>)collection);
}
USAGE:
public static void Main()
{
using (MyObjectContext context = new MyObjectContext())
{
//Using method 1 - collection provided as collection
var contacts1 =
context.Contacts.WhereIn(c => c.Name, GetContactNames());
//Using method 2 - collection provided statically
var contacts2 = context.Contacts.WhereIn(c => c.Name,
"Contact1",
"Contact2",
"Contact3",
"Contact4"
);
}
}
这篇关于'包含()'使用Linq到实体的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文