多条件查询
在写这个功能的时候 我遇到了很多问题,也走错了方向,最终我还是给做了出来
下面是代码
Db_laienTransEntities3 db = new Db_laienTransEntities3();
int pageSize = int.Parse(context.Request["rows"] ?? "10");
int pageIndex = int.Parse(context.Request["page"] ?? "1");
int tatalCount = 0;
JavaScriptSerializer js = new JavaScriptSerializer();
string data_ = "";
string pingtai = context.Request.Form["CN"];
DateTime? SJdate = null;
DateTime? SFdate = null;
DateTime? DYdate = null;
DateTime? JSDYdate = null;
DateTime? JDdate = null;
DateTime? JDZXdate = null;
DateTime? ZXdate = null;
DateTime? ZXJSdate = null;
IQueryable<NikeDTCDailyReport> user1 = db.NikeDTCDailyReport.Where(p => true);
if (context.Request.Form["SJdate"] != "" && context.Request.Form["SFdate"] != "")
{
#region
SJdate = Convert.ToDateTime(context.Request.Form["SJdate"]);
SFdate = Convert.ToDateTime(context.Request.Form["SFdate"]);
user1 = user1.Where(b => b.CN_Launch_Date > SJdate && b.CN_Launch_Date <= SFdate);
#endregion
}
if (context.Request.Form["DYdate"] != "" && context.Request.Form["JSDYdate"] != "")
{
DYdate = Convert.ToDateTime(context.Request.Form["DYdate"]);
JSDYdate = Convert.ToDateTime(context.Request.Form["JSDYdate"]);
user1 = user1.Where(b => b.Sample_Batch >= DYdate && b.Sample_Batch <= JSDYdate);
}
if (context.Request.Form["JDdate"] != "" && context.Request.Form["JDZXdate"] != "")
{
JDdate = Convert.ToDateTime(context.Request.Form["JDdate"]);
JDZXdate = Convert.ToDateTime(context.Request.Form["JDZXdate"]);
user1 = user1.Where(b => b.LW_Linguistic_Review >= JDdate && b.LW_Linguistic_Review <= JDZXdate);
}
if (context.Request.Form["ZXdate"] != "" && context.Request.Form["ZXJSdate"] != "")
{
ZXdate = Convert.ToDateTime(context.Request.Form["ZXdate"]);
ZXJSdate = Convert.ToDateTime(context.Request.Form["ZXJSdate"]);
user1 = user1.Where(b => b.Copy_Writing >= ZXdate && b.Copy_Writing <= ZXJSdate);
}
string kuanhao = "";
if (context.Request.Form["kuanhao"] != "")
{
kuanhao = context.Request.Form["kuanhao"];
user1 = user1.Where(b => b.Style == kuanhao);
}
string sehao = null;
if (context.Request.Form["sehao"] != "")
{
sehao = context.Request.Form["sehao"];
user1 = user1.Where(b => b.ProductCode.Contains(sehao));
}
if (pingtai != null && pingtai != "")
{
string[] ckarr = pingtai.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (ckarr[0] == "CN_Launch_Date")
{
user1 = user1.Where(b => b.CN_Launch_Date != null);
}
if (ckarr[0] == "HK_Launch_Date")
{
user1 = user1.Where(b => b.HK_Launch_Date != null);
}
if (ckarr[0] == "TMALL_Launch_Date")
{
user1 = user1.Where(b => b.TMALL_Launch_Date != null);
}
if (ckarr.Count() > 1)
{
if (ckarr[0].Equals("CN_Launch_Date") && ckarr[1].Equals("HK_Launch_Date"))
{
user1 = user1.Where(b => b.CN_Launch_Date != null && b.HK_Launch_Date != null);
}
if (ckarr[0].Equals("CN_Launch_Date") && ckarr[1].Equals("TMALL_Launch_Date"))
{
user1 = user1.Where(b => b.CN_Launch_Date != null && b.TMALL_Launch_Date != null);
}
if (ckarr[0].Equals("HK_Launch_Date") && ckarr[1].Equals("TMALL_Launch_Date"))
{
user1 = user1.Where(b => b.HK_Launch_Date != null && b.TMALL_Launch_Date != null);
}
}
if (ckarr.Count() > 2)
{
user1 = user1.Where(b => b.CN_Launch_Date != null && b.HK_Launch_Date != null && b.TMALL_Launch_Date != null);
}
//var quert = from c in user1 select new { c.CN_Launch_Date, c.HK_Launch_Date, c.TMALL_Launch_Date, c.Style, c.ProductCode, c.LW_Linguistic_Review, c.Copy_Writing, c.Sample_Batch,c.ID };
var quert = user1.ToList().Select(c => new { c.Style, c.CN_Launch_Date, c.HK_Launch_Date, c.TMALL_Launch_Date, ProductCode = c.ProductCode.Split('-')[1], c.LW_Linguistic_Review, c.Copy_Writing, c.Sample_Batch, c.ID }).ToList();
var rows = quert.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderBy(c => c.ID);
tatalCount = quert.Count();
data_ = js.Serialize(new { total = tatalCount, rows = rows });
}
else
{ //user1 = Excelpage<NikeDTCDailyReport>.LoadPageEntitles<Guid>(pageIndex, pageSize, out tatalCount, c => true, c => c.ID, false);
var quert = user1.ToList().Select(c => new { c.Style, c.CN_Launch_Date, c.HK_Launch_Date, c.TMALL_Launch_Date, ProductCode = c.ProductCode.Split('-')[1], c.LW_Linguistic_Review, c.Copy_Writing, c.Sample_Batch, c.ID }).ToList();
var rows = quert.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderBy(c => c.ID);
tatalCount = quert.Count();
data_ = js.Serialize(new { total = tatalCount, rows = rows });
}
context.Response.Clear();
context.Response.Write(data_);
context.Response.End();