1.linq的group by
var q = from p in db.Products group p by p.CategoryID into g where g.Count() >= 10 select new { g.Key, ProductCount = g.Count() };
var categories = from p in db.Products group p by new { p.CategoryID, p.SupplierID } into g select new { g.Key, g };
参考:http://developer.51cto.com/art/200909/150348.htm
var temp = redisClient.As<bsi_wcflog>(); var query = from w in temp.GetAll() where (w.hisID == hisid || hisid == 0)&&w.paid=='0' group w by new{Account = w.Account,Method = w.Method,payprice = w.payprice} into g select new WcfLogModel { Account = g.Key.Account, Method = g.Key.Method, eachprice = g.Key.payprice, TotalPayPrice = g.Sum(qu=>qu.payprice*qu.exTimes), TotalCount = g.Sum(qu=>qu.exTimes), SumSuccess = g.Count(qu=>qu.isSuccess==true), SumFail = g.Count(qu => qu.isSuccess == false) };
2.linq 根据一个字段去重
DALTask dalwx133 = new DALTask(); List<inf_wx133> lstwx133 = dalwx133.getSIDbyflightNo(flightNo, flightdate.ToString("yyyy-MM-dd")).Distinct(new wx133Comparer()).ToList(); class wx133Comparer : EqualityComparer<inf_wx133> { public override bool Equals(inf_wx133 x, inf_wx133 y) { return x.citypair == y.citypair; } public override int GetHashCode(inf_wx133 obj) { return obj.citypair.GetHashCode(); } }