Linq 多条件组合拼接排序
来源:互联网 发布:apache shiro文档 编辑:程序博客网 时间:2024/04/29 18:23
<%@ WebHandler Language="C#" Class="Huifang" %>using System;using System.Web;using Uzai.Shop.Entity.comment;using System.Collections.Generic;using Uzai.Shop.BLL.comment;using System.Linq;using System.Web.Script.Serialization;/// <summary>/// tanyong 2013-09-18/// </summary>public class Huifang : IHttpHandler { #region 自定义变量 TalkBackBLL talkbll = new TalkBackBLL(); #endregion public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string startDate = context.Request.QueryString["sDate"]; string endDate = context.Request.QueryString["eDate"]; string index = context.Request.QueryString["act"]; List<ProTalkBackEntity> list = talkbll.GetProTalkBack(Convert.ToDateTime(startDate), Convert.ToDateTime(endDate)); List<ProTalkBackEntity> proList = new List<ProTalkBackEntity>(); var query = list.GroupBy(t => t.ProductName).Select(p => new { ProductName = p.Key, hascomment = p.Sum(x => x.hascomment), Ding = p.Sum(x => x.Ding), Zan = p.Sum(x => x.Zan) }); if (index == "0")//点评 query = query.OrderByDescending(t => t.hascomment); else if (index == "1")//顶 query = query.OrderByDescending(t => t.Ding); else//赞 query = query.OrderByDescending(t=>t.Zan); query = query.Take(10).ToList();// 赞,顶,点评最大的前10条数据 foreach (var item in query) { proList.Add(new ProTalkBackEntity() { ProductName = item.ProductName, hascomment = item.hascomment, Ding = item.Ding, Zan = item.Zan }); } JavaScriptSerializer json = new JavaScriptSerializer(); string result = json.Serialize(proList); context.Response.Write(result); } public bool IsReusable { get { return false; } }}