学习并使用了两种linq to entity 的实现sql关键字in的查询方法
2013-08-01 19:32
971 查看
//构造Lambda语句
private static Expression<Func<TElement, bool>> BuildWhereInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values)
{
ParameterExpression p = propertySelector.Parameters.Single();
if (!values.Any())
return e => false;
var equals = values.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
//调用
string[] ids = new string[]{"id1","id2","id3"};
db.ProjectFiles.Where(BuildWhereInExpression<Profile,int>(v=>v.Id,ids);
//封装方法
public static IQueryable<TElement> WhereIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, params TValue[] values)
{
return source.Where(BuildWhereInExpression(propertySelector, values));
}
//直接使用下列方法调用
string[] ids = new string[]{"id1","id2","id3"};
db.ProjectFiles.WhereIn(c => c.Id,ids);
//字符串构造
//这个方法比较简单
string[] ids = new string[]{"id1","id2","id3"};
cstIds=cstIds.Replace(",","','");
var query = db.ProjectFiles.Where("it.Id in {'" + cstIds + "'}");
private static Expression<Func<TElement, bool>> BuildWhereInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values)
{
ParameterExpression p = propertySelector.Parameters.Single();
if (!values.Any())
return e => false;
var equals = values.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
//调用
string[] ids = new string[]{"id1","id2","id3"};
db.ProjectFiles.Where(BuildWhereInExpression<Profile,int>(v=>v.Id,ids);
//封装方法
public static IQueryable<TElement> WhereIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, params TValue[] values)
{
return source.Where(BuildWhereInExpression(propertySelector, values));
}
//直接使用下列方法调用
string[] ids = new string[]{"id1","id2","id3"};
db.ProjectFiles.WhereIn(c => c.Id,ids);
//字符串构造
//这个方法比较简单
string[] ids = new string[]{"id1","id2","id3"};
cstIds=cstIds.Replace(",","','");
var query = db.ProjectFiles.Where("it.Id in {'" + cstIds + "'}");
相关文章推荐
- 学习并使用了两种linq to entity 的实现sql关键字in的查询方法
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #3 -- LINQ-to-SQL、EntitySQL、查询产生器方法(Query builder)三种语法
- asp.net MVC + linq to Entity简单教程(四)linq to Entity查询中其它方法的使用
- Linq to sql 实现多条件的动态查询(方法一)
- [转自JeffreyZhao]在LINQ to SQL中使用Translate方法以及修改查询用SQL
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL
- [转自JeffreyZhao]在LINQ to SQL中使用Translate方法以及修改查询用SQL
- LINQ to Entities 实现sql 关键字"In"方式总结
- Silverlight + WCF使用Linq to SQL以及ADO.NET Entity Data Model更新数据库子表方法
- LINQ to Entities 实现sql 关键字"In"方式总结
- 如何使用 Linq to Entity 实现 in 操作
- LINQ to Entities 实现sql 关键字"In"方式总结
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL (转)
- LINQ学习笔记(三)之LINQ to SQL 查询语句使用方法一
- LINQ to Entities 实现sql 关键字"In"方式总结
- MVC中使用LINQ TO SQL实现多表查询及分页
- LINQ to Entities 实现sql 关键字"In"方式总结
- Linq to entity 中 实现 Sql 中的like 的两个方法小解
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL
- LINQ to Entities 实现sql 关键字"In"方式总结