上一篇实现了基本的打印report功能,现在Report的要求变化了
订单号,部门名称,设备名称 不会在Detail里重复出现了。
所以一共会有4张report被打印
第一张 第一条 泡沫红茶
第二张 第二条 茉香绿茶
第三张 蓝色圈起来的 同一个厨房 同一个打印机上,打印效果下图
第4张 5,6两条
使用LINQ TO DATATABLE 将数据分离,逐个打印。如下代码,没什么意思了。
StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true); // Append
db.Log = sw;
var query = from departmentInfo in db.Department_Infos
join datasource in db.DataSources
on departmentInfo.Department_ID
equals datasource.Department_Id into deps
join equipment in db.Equipment_Infos
on departmentInfo.Department_ID equals equipment.Equipment_DPID into equips
from c in equips.DefaultIfEmpty()
from d in deps.DefaultIfEmpty()
select new
{
订单号 = d.orderFormId,
商品名称 = d.MS_Name,
部门名称 = departmentInfo.Department_Name,
设备名称 = c.Equipment_Name
};
//节省数据库开销 query存入 Datatable ,直接对datatable做 查询
DataTable queryResult = new DataTable();
CustomLINQtoDataSetMethods.CopyToDataTable(query, queryResult, LoadOption.PreserveChanges);
//筛选出 打印机名 不重复的打印机名
List<string> printerNames =
(from p in queryResult.AsEnumerable()
where (string)p["订单号"] == "20080509_0001" //这里的2个datatable列一定要强制转换,否则结果不对
select (string)p["设备名称"]).Distinct().ToList<string>();
Kitchen.Kitchen kitchen = new Kitchen.Kitchen();
DataTable table = new DataTable();
// 为每个打印机获取 相应 数据源datatable
foreach (var printerName in printerNames)
{
var spreatReport = from p in queryResult.AsEnumerable()
where (string)p["设备名称"] == printerName
select p;
//查询结果转换 LINQ TO DATATABLE
//如果用第一章找到的扩展方法使用会出错(反射没有找到类型)。使用官方的。
table = System.Data.DataTableExtensions.CopyToDataTable(spreatReport);
kitchen = Kitchen.Kitchen.GetKtchenReportSource(table);
Response.Write("已打印到" + printerName + "</br>");
kitchen.Print("Foxit PDF Printer"); //没有数据库里的那么多打印机 凑活用用
}
GridView1.DataSource = queryResult;
GridView1.DataBind();
sw.Close();
db.Log = sw;
var query = from departmentInfo in db.Department_Infos
join datasource in db.DataSources
on departmentInfo.Department_ID
equals datasource.Department_Id into deps
join equipment in db.Equipment_Infos
on departmentInfo.Department_ID equals equipment.Equipment_DPID into equips
from c in equips.DefaultIfEmpty()
from d in deps.DefaultIfEmpty()
select new
{
订单号 = d.orderFormId,
商品名称 = d.MS_Name,
部门名称 = departmentInfo.Department_Name,
设备名称 = c.Equipment_Name
};
//节省数据库开销 query存入 Datatable ,直接对datatable做 查询
DataTable queryResult = new DataTable();
CustomLINQtoDataSetMethods.CopyToDataTable(query, queryResult, LoadOption.PreserveChanges);
//筛选出 打印机名 不重复的打印机名
List<string> printerNames =
(from p in queryResult.AsEnumerable()
where (string)p["订单号"] == "20080509_0001" //这里的2个datatable列一定要强制转换,否则结果不对
select (string)p["设备名称"]).Distinct().ToList<string>();
Kitchen.Kitchen kitchen = new Kitchen.Kitchen();
DataTable table = new DataTable();
// 为每个打印机获取 相应 数据源datatable
foreach (var printerName in printerNames)
{
var spreatReport = from p in queryResult.AsEnumerable()
where (string)p["设备名称"] == printerName
select p;
//查询结果转换 LINQ TO DATATABLE
//如果用第一章找到的扩展方法使用会出错(反射没有找到类型)。使用官方的。
table = System.Data.DataTableExtensions.CopyToDataTable(spreatReport);
kitchen = Kitchen.Kitchen.GetKtchenReportSource(table);
Response.Write("已打印到" + printerName + "</br>");
kitchen.Print("Foxit PDF Printer"); //没有数据库里的那么多打印机 凑活用用
}
GridView1.DataSource = queryResult;
GridView1.DataBind();
sw.Close();
会有个限制:打印机不能重名。不然查询的时候结果就会不对,那么打印的数据也会不正确。
查阅:sunrack的LINQ TO DATATABLE
步步为营VS 2008 + .NET 3.5(6) - LINQ查询操作符之Distinct、Union、Concat、Intersect、Except、Skip、Take、SkipWhile、TakeWhile、Single、SingleOrDefault、Reverse、SelectMany
源代码 VS2008