日期:2014-05-20  浏览次数:20839 次

有用过System.Linq.Dynamic 进行动态LINQ文的吗? 其中动态LINQ 中SQL中的IN该怎么做?? 下面几种写法都不对.请教??
有用过System.Linq.Dynamic 进行动态LINQ文的吗? 其中动态LINQ 中SQL中的IN该怎么做?? 下面几种写法都不对.请教??


string[] strList = junUserList.ToArray();

① 错
where1 = where1 + "a.UserID in (\"0000001010\",\"0000001040\")) ";

② 错.
where1 = where1 + " ((@" + j + ").Contains(a.UserID)) ";


smartfolderTBl.Where(where1,strList).Select(strSelect);



改怎么样做呀.?????

------解决方案--------------------
/// <summary>
/// 根据条件查询所有信息
/// </summary>
/// <param name="bargainId">产品编号</param>
/// <param name="modelQ">条件列表</param>
/// <param name="pageSize">分页大小</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageCount">总页数</param>
/// <param name="Counts">总记录数</param>
/// <returns></returns>
public IQueryable GetList(int bargainId,SJJ.WhereModel.furniture_bargain_review modelQ, int pageSize, int pageIndex, out int pageCount, out int Counts)
{
//初始化数据库连接字符串·开始
setConnectionString();
//初始化数据库连接字符串·结束
pageCount = Counts = 0;
var objQueryWhere = linqWhere.True<SJJ.LINQ.furniture_bargain_review>();

#region 生成判断条件

if (modelQ != null)
{
//评论·时间
if (modelQ.furniture_bargain_review_Date_begin != null && modelQ.furniture_bargain_review_Date_end != null)
{
objQueryWhere = objQueryWhere.And(p => p.furniture_bargain_review_Date >= modelQ.furniture_bargain_review_Date_begin);
objQueryWhere = objQueryWhere.And(p => p.furniture_bargain_review_Date <= modelQ.furniture_bargain_review_Date_end);
}
//评论·名称
if (!string.IsNullOrEmpty(modelQ.furniture_bargain_review_Name))
{
objQueryWhere = objQueryWhere.And(p => p.furniture_bargain_review_Name.Contains(modelQ.furniture_bargain_review_Name));
}
//评论·Email
if (!string.IsNullOrEmpty(modelQ.furniture_bargain_review_Email))
{
objQueryWhere = objQueryWhere.And(p => p.furniture_bargain_review_Email.Contains(modelQ.furniture_bargain_review_Email));
}
//评论·内容
if (!string.IsNullOrEmpty(modelQ.furniture_bargain_review_Body))
{
objQueryWhere = objQueryWhere.And(p => p.furniture_bargain_review_Body.Contains(modelQ.furniture_bargain_review_Body));
}
}

#endregion 生成判断条件

var localTable = (from objTableA in objDataContext.furniture_bargain_review.Where(objQueryWhere)
where objTableA.furniture_bargain_Id == bargainId
join objTableB in objDataContext.furniture_bargain on objTableA.furniture_bargain_Id equals objTableB.furniture_bargain_Id
select new
{
objTableA.furniture_bargain_review_Id,
objTableA.furniture_bargain_review_Date,
objTableB.furniture_bargain_Id,
objTableB.furniture_bargain_Name,
objTableA.furniture_bargain_review_Name,
objTableA.furniture_bargain_review_Email,
objTableA.furniture_bargain_review_Ip,
objTableA.furniture_bargain_review_Body
}).OrderByDescending(p => p.furniture_bargain_review_Date);
Counts = localTable.Count();
if (Counts % pageSize != 0)
{
pageCount = Counts / pageSize + 1;
}
else
{
pageCount = Counts / pageSize;
}
return localTable.Skip((pageIndex - 1) * pageSize).Take(pageSize);
}
------解决方案--------------------
1、添加类PredicateExtensions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Www24100Net.PlayingWithLinq
{
public static class PredicateExtensions
{

public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1,
Expression<Func<T, bool>> expression2)
{

var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

return Expression.Lambda<Func<T, bool>>

(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);
}



public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
Expression<Func<T, bool>> expression2)
{

var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

return Expression.Lambda<Func<T, bool>>

(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);

}

}

}

2、Linq动态查询
private void bind(int pageIndex)
{
if(pageIndex < 1){pageIndex = 1;}

var predicate = PredicateExtensions.True<CityName>();

string _fprovince = "福建";
if (!string.IsNullOrEmpty(_fprovince))
{
predicate = predicate.And(p => p.FProvince.Contains(_fprovince));
}

string _fcity = "厦门";
if (!string.IsNullOrEmpty(_fcity))
{
predicate = predicate.And(p => p.FCity.Contains(_fcity));
}

string _ftype = "联通";
if (!string.IsNullOrEmpty(_ftype))
{
predicate = predicate.And(p => p.FType.Contains(_ftype));
}

DataClassesDataContext DBCity = new DataClassesDataContext();

var query = DBCity.CityName.Where(predicate);

AspNetPager1.RecordCount = query.Count();
this.Label1.Text = AspNetPager1.RecordCount.ToString();

intStartRow = intPageSize * (pageIndex-1) ;
intEndRow = pageIndex * intPageSize;

GridView1.DataSource = query.OrderBy(p => p.FSimseg).Skip(intStartRow).Take(intPageSize);
GridView1.DataBind();
}