自己练习的
switch (productDataAnalysisQuery.DataType)
{
case :
var data = (from hp in GPEcontext.hbl_product
join hcim in
from hci in GPEcontext.hbl_change_info
where hci.LotNum == && hci.ChangeData <
&& hci.CreatedTime >= beginDate && hci.CreatedTime <= endDate
group hci by new { hci.ProductId } into G
select new
{
ProductId = G.Key.ProductId,
Amount = G.Sum(item => Math.Abs(item.ChangeData.Value))
} on hp.id equals hcim.ProductId
orderby hcim.Amount descending
select new ProductDataAnalysisodel
{
ProductName = hp.ProductName,
ProductCode = hp.ProductCode,
Amount = hcim.Amount
}).Take();
pageModel.Models = data.FindBy(item => true, productDataAnalysisQuery.PageNo, productDataAnalysisQuery.PageSize, out total, item => item.Amount, false);
break;
case :
var data2 = (from hp in GPEcontext.hbl_product
join hcim in
from hci in GPEcontext.hbl_change_info
where hci.LotNum == && hci.ChangeData <
&& hci.CreatedTime >= beginDate && hci.CreatedTime <= endDate
group hci by new { hci.ProductId } into G
select new
{
ProductId = G.Key.ProductId,
Amount = G.Sum(item => Math.Abs(item.ChangeData.Value))
} on hp.id equals hcim.ProductId
orderby hcim.Amount
select new ProductDataAnalysisodel
{
ProductName = hp.ProductName,
ProductCode = hp.ProductCode,
Amount = hcim.Amount
}).Take();
pageModel.Models = data2.FindBy(item => true, productDataAnalysisQuery.PageNo, productDataAnalysisQuery.PageSize, out total, item => item.Amount, false);
break;
case :
var data3 = (from hp in GPEcontext.hbl_product
join hcim in
from hci in GPEcontext.hbl_change_info
where hci.LotNum == && hci.ChangeData <
&& hci.CreatedTime >= beginDate && hci.CreatedTime <= endDate
group hci by new { hci.ProductId } into G
select new
{
ProductId = G.Key.ProductId,
Amount = G.Count()
} on hp.id equals hcim.ProductId
orderby hcim.Amount descending
select new ProductDataAnalysisodel
{
ProductName = hp.ProductName,
ProductCode = hp.ProductCode,
Amount = hcim.Amount
}).Take();
pageModel.Models = data3.FindBy(item => true, productDataAnalysisQuery.PageNo, productDataAnalysisQuery.PageSize, out total, item => item.Amount, false); break;
case :
var data4 = (from hp in GPEcontext.hbl_product
join hcim in
from hci in GPEcontext.hbl_change_info
where hci.LotNum == && hci.ChangeData <
&& hci.CreatedTime >= beginDate && hci.CreatedTime <= endDate
group hci by new { hci.ProductId } into G
select new
{
ProductId = G.Key.ProductId,
Amount = G.Count()
} on hp.id equals hcim.ProductId
orderby hcim.Amount
select new ProductDataAnalysisodel
{
ProductName = hp.ProductName,
ProductCode = hp.ProductCode,
Amount = hcim.Amount
}).Take();
pageModel.Models = data4.FindBy(item => true, productDataAnalysisQuery.PageNo, productDataAnalysisQuery.PageSize, out total, item => item.Amount, false); break;
case :
var data5 = (from hp in GPEcontext.hbl_product
join hs in
from hps2 in GPEcontext.hbl_product_stock
join p in
from hps in GPEcontext.hbl_product_stock
group hps by new { hps.ProductID } into G
select new
{
ProductId = G.Key.ProductID,
CreatedTime = G.Max(p => p.CreatedTime)
}
on new { A = hps2.ProductID, B = hps2.CreatedTime } equals new { A = p.ProductId, B = p.CreatedTime }
select new
{
ProductId = hps2.ProductID,
Amount = hps2.Stock
}
on hp.id equals hs.ProductId
orderby hs.Amount descending
select new ProductDataAnalysisodel
{
ProductName = hp.ProductName,
ProductCode = hp.ProductCode,
Amount = hs.Amount.Value
}).Take();
pageModel.Models = data5.FindBy(item => true, productDataAnalysisQuery.PageNo, productDataAnalysisQuery.PageSize, out total, item => item.Amount, false); break;
case :
var data6 = (from hp in GPEcontext.hbl_product
join hs in
from hps2 in GPEcontext.hbl_product_stock
join p in
from hps in GPEcontext.hbl_product_stock
group hps by new { hps.ProductID } into G
select new
{
ProductId = G.Key.ProductID,
CreatedTime = G.Max(p => p.CreatedTime)
}
on new { A = hps2.ProductID, B = hps2.CreatedTime } equals new { A = p.ProductId, B = p.CreatedTime }
select new
{
ProductId = hps2.ProductID,
Amount = hps2.Stock
}
on hp.id equals hs.ProductId
orderby hs.Amount
select new ProductDataAnalysisodel
{
ProductName = hp.ProductName,
ProductCode = hp.ProductCode,
Amount = hs.Amount.Value
}).Take();
pageModel.Models = data6.FindBy(item => true, productDataAnalysisQuery.PageNo, productDataAnalysisQuery.PageSize, out total, item => item.Amount, false); break;
}
Linq使用Group By
.简单形式: var q =
from p in db.Products
group p by p.CategoryID into g
select g;
语句描述:Linq使用Group By按CategoryID划分产品。 说明:from p in db.Products 表示从表中将产品对象取出来。group p by p.CategoryID into g表示对p按CategoryID字段归类。其结果命名为g,一旦重新命名,p的作用域就结束了,所以,最后select时,只能select g。 .最大值 var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
MaxPrice = g.Max(p => p.UnitPrice)
};
语句描述:Linq使用Group By和Max查找每个CategoryID的最高单价。 说明:先按CategoryID归类,判断各个分类产品中单价最大的Products。取出CategoryID值,并把UnitPrice值赋给MaxPrice。 .最小值 var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
MinPrice = g.Min(p => p.UnitPrice)
};
语句描述:Linq使用Group By和Min查找每个CategoryID的最低单价。 说明:先按CategoryID归类,判断各个分类产品中单价最小的Products。取出CategoryID值,并把UnitPrice值赋给MinPrice。 .平均值 var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
AveragePrice = g.Average(p => p.UnitPrice)
};
语句描述:Linq使用Group By和Average得到每个CategoryID的平均单价。 说明:先按CategoryID归类,取出CategoryID值和各个分类产品中单价的平均值。 .求和 var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
TotalPrice = g.Sum(p => p.UnitPrice)
};

今天遇到了一个问题,操作一个DataTable,DataTable的表结构如下:

  1. Id,Name,Math,Chinese

此表对应的数据如下图:

要求合并后的效果如下图:

自己就想到了for循环的实现办法,也想用Linq可是不知道怎么写。后来msdn了一下,写出了如下的代码:

var query = from c in ds.Tables[].AsEnumerable()
group c by c.Field<int>("Name")
into s
select new
{
ID = s.Select(m => m.Field<int>("ID").ToString()),
Name = s.Select(m => m.Field<string>("Name").ToString()),
Math = s.Sum(p => p.Field<decimal>("Math")),
Chinese = s.Sum(p => p.Field<decimal>("Chinese"))
};       //法一:
        DataTable tbl = tableA1.Clone();
query.ToList().ForEach(p => tbl.Rows.Add(p.pingming, p.guige,p.biaohao,p.shuliang));
  //法二:
    DataTable dtss = new DataTable(); 
dtss.Columns.Add(new DataColumn("ID", typeof(string)));
dtss.Columns.Add(new DataColumn("Name", typeof(string)));
dtss.Columns.Add(new DataColumn("Math", typeof(string)));
dtss.Columns.Add(new DataColumn("Chinese", typeof(string)));
query.ToList().ForEach(p => dtss.Rows.Add(p.ID, p.Name, p.Math, p.Chinese ));

注意:linq结果可以用AsEnumerable立刻执行查询以及ToList ToArray转换成数组/集合

linq 常用语句的更多相关文章

  1. SQL server 常用语句

    SQL Server中常用的SQL语句   1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...

  2. mysql常用语句总结

    1.创建语句 CREATE DATABASE database_name //创建数据库 //删表 DROP TABLE IF EXISTS `t_social_user_extend`; //建表C ...

  3. JS常用语句

    JavaScript常用语句 1.document.write("");    输出语句 2.JS中的注释为   // 3.传统的HTML文档顺序是:     document-& ...

  4. velocity常用语句速查表

    velocity常用语句 * 变量定义 #set($directoryRoot = "www" ) * #if($!list.size() != 0) //判断list不为空 #f ...

  5. MySQL 常用语句 (汇集)

    原文地址:MySql常用语句作者:wuyanle 一.mysql常用语句 创建,删除和最基本查询: 显示数据库  mysql->show databases; 创建数据库  mysql-> ...

  6. mysql数据库常用语句2

    关于mysql常用语句的整理,上一篇涉及到ddl.dml以及一些简单的查询语句. 1:mysql分页查询 select * from table_name limit 5,10;   从下标为5元素查 ...

  7. mysqldump的常用语句及各参数详解

    mysqldump的常用语句及各参数详解 分类: MySQL 2011-01-11 17:55 1368人阅读 评论(0) 收藏 举报 数据库mysql服务器tableinsertdatabase m ...

  8. mysql学习一 常用语句

    操作系统为windows  1 启动关闭mysql服务 //windows mysqld --console //开启mysql服务 mysqladmin -uroot shutdown //关闭my ...

  9. mo系统常用语句

    mo系统常用语句 一.总结 一句话总结: 1.语言:双语设置(繁体,英语)语句? {:chooseLanguage("確定要刪除么","Are you sure you ...

随机推荐

  1. Openstack:ice-house安装过程

    #apt-get install ntpdpkg-reconfigure tzdata --> Asia -->Shuanghai #apt-get install python-mysq ...

  2. Java日期处理类的lenient属性

    这个特性很坑爹:@Test public void test() throws ParseException { SimpleDateFormat df = new SimpleDateFormat( ...

  3. 数组链表下标指针map list

    1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间 ...

  4. JavaScript AJAX stream 流式显示

      当使用AJAX进行信息交互的时候,如果服务器返回的信息比较大,那么相对于传送完成之后的统一显示,流式显示就比较友好了. 流式实现 原理就是设置定时器,定时的查看AJAX对象的状态并更新内容,如果传 ...

  5. require.js的用法

    我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文 ...

  6. [转]每天一个linux命令目录

    [转]每天一个linux命令目录 http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每 ...

  7. 在package.json中配置Script执行npm run tslint报错问题

    今天在学习tslint的时候,按照git clone下angular2-webpack-starter的代码执行npm run lint时,虽然代码进行了检测,但检测完成后npm始终报错, //pac ...

  8. NewRelicAgent(CustomAnalyticEvent.cxx.o), building for iOS simulator, but linking in object file built for OSX, for architecture x8(botched)

    昨天遇到一个问题,在项目swift1.2适配swift2.0的过程中,修改完毕之后,运行报错如下: /Pods/NewRelicAgent/NewRelic_iOS_Agent_5.1.0/NewRe ...

  9. struts1 和 struts2中Action什么时候实例化

    精帖1:http://blog.csdn.net/lfsf802/article/details/7277013 精帖1:http://blog.csdn.net/wmj2003/article/de ...

  10. MyEclipse 关闭拼写检查、JavaScript的检查Build、xml、JSP的Bulid检查

    前言 MyEclipse 的拼写检查.JavaScript的检查Build.xml.JSP的Bulid检查很讨厌,有时不仅会一直build卡住,而且明明是对的它却报错,示例: 关闭方法 1.关闭拼写检 ...