建立一个动态构建查询语句的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Linq.Expressions;
namespace SJJ.DataAccess
{
public static class linqWhere
{
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>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}
}
13 个解决方案
#1
第二步:
建立相关联的实体类:
using System;
namespace SJJ.WhereModel
{
/// <summary>
/// 家具频道·特价信息·评论
/// </summary>
[Serializable]
public class furniture_bargain_review
{
#region 成员方法
/// <summary>
/// 评论时间·始
/// </summary>
public DateTime furniture_bargain_review_Date_begin { get; set; }
/// <summary>
/// 评论时间·末
/// </summary>
public DateTime furniture_bargain_review_Date_end { get; set; }
/// <summary>
/// 评论用户
/// </summary>
public string furniture_bargain_review_Name { get; set; }
/// <summary>
/// 电子邮件
/// </summary>
public string furniture_bargain_review_Email { get; set; }
/// <summary>
/// 评论内容
/// </summary>
public string furniture_bargain_review_Body { get; set; }
#endregion
}
}
建立相关联的实体类:
using System;
namespace SJJ.WhereModel
{
/// <summary>
/// 家具频道·特价信息·评论
/// </summary>
[Serializable]
public class furniture_bargain_review
{
#region 成员方法
/// <summary>
/// 评论时间·始
/// </summary>
public DateTime furniture_bargain_review_Date_begin { get; set; }
/// <summary>
/// 评论时间·末
/// </summary>
public DateTime furniture_bargain_review_Date_end { get; set; }
/// <summary>
/// 评论用户
/// </summary>
public string furniture_bargain_review_Name { get; set; }
/// <summary>
/// 电子邮件
/// </summary>
public string furniture_bargain_review_Email { get; set; }
/// <summary>
/// 评论内容
/// </summary>
public string furniture_bargain_review_Body { get; set; }
#endregion
}
}
#2
第三步:
开始调用
/// <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);
}
开始调用
/// <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);
}
#3
以上是本人的一个解决方案
还大家指正其中的不足之处
最好能改进,呵
还大家指正其中的不足之处
最好能改进,呵
#4
mark
#5
mark
#6
不知道是我做得不好?
还是那位高人有更好的解决方案?
希望能分享……
还是那位高人有更好的解决方案?
希望能分享……
#7
郁闷,没做过。。。
#8
楼主的动态查询类怎么和一个老外写的一模一样啊。。。。
#9
mark
#10
学习了
#11
ding
#12
mark,收藏了。。。。。。。。。
#13
查询类是老外写的
#1
第二步:
建立相关联的实体类:
using System;
namespace SJJ.WhereModel
{
/// <summary>
/// 家具频道·特价信息·评论
/// </summary>
[Serializable]
public class furniture_bargain_review
{
#region 成员方法
/// <summary>
/// 评论时间·始
/// </summary>
public DateTime furniture_bargain_review_Date_begin { get; set; }
/// <summary>
/// 评论时间·末
/// </summary>
public DateTime furniture_bargain_review_Date_end { get; set; }
/// <summary>
/// 评论用户
/// </summary>
public string furniture_bargain_review_Name { get; set; }
/// <summary>
/// 电子邮件
/// </summary>
public string furniture_bargain_review_Email { get; set; }
/// <summary>
/// 评论内容
/// </summary>
public string furniture_bargain_review_Body { get; set; }
#endregion
}
}
建立相关联的实体类:
using System;
namespace SJJ.WhereModel
{
/// <summary>
/// 家具频道·特价信息·评论
/// </summary>
[Serializable]
public class furniture_bargain_review
{
#region 成员方法
/// <summary>
/// 评论时间·始
/// </summary>
public DateTime furniture_bargain_review_Date_begin { get; set; }
/// <summary>
/// 评论时间·末
/// </summary>
public DateTime furniture_bargain_review_Date_end { get; set; }
/// <summary>
/// 评论用户
/// </summary>
public string furniture_bargain_review_Name { get; set; }
/// <summary>
/// 电子邮件
/// </summary>
public string furniture_bargain_review_Email { get; set; }
/// <summary>
/// 评论内容
/// </summary>
public string furniture_bargain_review_Body { get; set; }
#endregion
}
}
#2
第三步:
开始调用
/// <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);
}
开始调用
/// <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);
}
#3
以上是本人的一个解决方案
还大家指正其中的不足之处
最好能改进,呵
还大家指正其中的不足之处
最好能改进,呵
#4
mark
#5
mark
#6
不知道是我做得不好?
还是那位高人有更好的解决方案?
希望能分享……
还是那位高人有更好的解决方案?
希望能分享……
#7
郁闷,没做过。。。
#8
楼主的动态查询类怎么和一个老外写的一模一样啊。。。。
#9
mark
#10
学习了
#11
ding
#12
mark,收藏了。。。。。。。。。
#13
查询类是老外写的