大佬教程收集整理的这篇文章主要介绍了使用Linq to Entities的“ Contains()”解决方法?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
EF≥4 Contains
直接支持(checkoutAny
),因此您不需要任何解决方法。
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);
}
用法:
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"
);
}
}
我正在尝试创建一个查询,该查询使用Silverlight ADO.Net数据服务客户端api(并因此使用Linq To
Entities)在where子句中使用ID列表。有人知道不支持“包含”的解决方法吗?
我想做这样的事情:
List<long?> txnIds = new List<long?>();
// Fill list
var q = from t in svc.opentransaction
where txnIds.Contains(t.opentransactionId)
SELEct t;
试过这个:
var q = from t in svc.opentransaction
where txnIds.Any<long>(tt => tt == t.opentransactionId)
SELEct t;
但是得到了“不支持方法’Any’”。
以上是大佬教程为你收集整理的使用Linq to Entities的“ Contains()”解决方法?全部内容,希望文章能够帮你解决使用Linq to Entities的“ Contains()”解决方法?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。