如何将Linq结果集转换为数据表以绑定gridview [英] How to convert Linq result set into datatable to bind gridview
本文介绍了如何将Linq结果集转换为数据表以绑定gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用linq结果绑定数据表,然后将数据表与gridview绑定.怎么做
I want to bind datatable with linq result then datatable will be binded with gridview. how to do it
if (ListBoxFrom.Items.Count > 0)
{
for (int i = 0; i < ListBoxFrom.Items.Count; i++)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
if (ListBoxFrom.Items[i].Selected)
{
string SelectedItem = ListBoxFrom.Items[i].Text;
CnmtDataContext cnmt = new CnmtDataContext();
var SummingTotal = from b in cnmt.CONSIGNMENTs.AsEnumerable()
// where b.CODE_NAME == SelectedItem && b.TOO == ListBoxToo.SelectedValue && b.CNDT >= Convert.ToDateTime(txtFromDate.Text).Date && b.CNDT <= Convert.ToDateTime(txtTooDate.Text).Date
where b.CODE_NAME == SelectedItem
//& p.FREIGHT >= Convert.ToInt32(txtFreight.Text)
group b by b.CODE_NAME into g
select new
{
Branch_Name = g.Key,
Cnmt = g.Count(),
TWeight = g.Sum(twgt => Convert.ToInt32(twgt.AWGHT)),
CWeight = g.Sum(cwgt => Convert.ToInt32(cwgt.CWGHT)),
Freight = g.Sum(frt => frt.FREIGHT),
Risk = g.Sum(risk => risk.RC),
Cartridge = g.Sum(cartridge => cartridge.CARTRAGE),
Osc = g.Sum(osc => osc.OSC),
RC = g.Sum(rc => rc.RC),
Hamali = g.Sum(hamali => hamali.HAMALI),
Other = g.Sum(other => other.OTHER),
Insu = g.Sum(ins => ins.INSURANCE),
BC = g.Sum(bc => bc.BILTYCHARGE),
SC = g.Sum(sc => sc.SC),
PF = g.Sum(pf => pf.PF),
Claim = g.Sum(claim => claim.CLAIM),
Total = g.Sum(total => total.TOTAL),
Stax = g.Sum(stax => stax.STAX),
GTotal = g.Sum(Gtot => Gtot.GTOTAL),
};
GrvEnquiry.DataSource = SummingTotal;
GrvEnquiry.DataBind();
推荐答案
尝试更改以下内容:
Try changing the following :
...
GrvEnquiry.DataSource = SummingTotal.ToList();
您可以将结果集转换为List:
You can convert the result set to List:
IQueryable<Product> products = from p in dbContext.Products
orderby p.ProductName descending
select p;
List<product> productsList = products.ToList();</product>
GrvProducts.DataSource = productsList ;
GrvProducts.DataBind();
或者,如果您想将列表转换为数据表,然后绑定gridview,请检查此文章以了解如何将列表转换为数据表.
http://www.c-sharpcorner.com/UploadFile /1a81c5/list-to-datatable-converter-using-C-Sharp/ [
or if you want to convert your list to datatable and then bind gridview, then check this articl to know how to convert list to datatable.
http://www.c-sharpcorner.com/UploadFile/1a81c5/list-to-datatable-converter-using-C-Sharp/[^]
以此方式重写Linq查询
var SummingTotal =来自cnmt中的b
其中b.CODE_NAME == SelectedItem
将b.CODE_NAME的b组划分为g
选择新
{
Branch_Name = g.Key,
Cnmt = g.Count(),
TWeight = g.Sum(twgt => Convert.ToInt32(twgt.AWGHT)),
CWeight = g.Sum(cwgt => Convert.ToInt32(cwgt.CWGHT)),
货运= g.总和(frt => frt.FREIGHT),
风险= g.Sum(风险=> risk.RC),
碳粉盒= g.Sum(碳粉盒=>碳粉盒.CARTRAGE),
Osc = g.Sum(osc => osc.OSC),
RC = g.Sum(rc => rc.RC),
Hamali = g.Sum(hamali => hamali.HAMALI),
其他= g.Sum(other => other.OTHER),
Insu = g.Sum(ins => ins.INSURANCE),
BC = g.Sum(bc => bc.BILTYCHARGE),
SC = g.Sum(sc => sc.SC),
PF = g.Sum(pf => pf.PF),
Claim = g.Sum(claim => Claim.CLAIM),
总计= g.Sum(总计=>总计.TOTAL),
Stax = g.Sum(stax => stax.STAX),
GTotal = g.Sum(Gtot => Gtot.GTOTAL),
};
GrvEnquiry.DataSource = SummingTotal.ToDataTable(); //调用以下函数将linq查询结果转换为数据表...
GrvEnquiry.DataBind();
//比编写以下函数将linq结果转换为数据表..
公共静态DataTable ToDataTable(此IEnumerable 源)
{
PropertyInfo []属性= typeof(T).GetProperties();
DataTable输出=新的DataTable();
foreach(属性中的var prop)
{
output.Columns.Add(prop.Name,prop.PropertyType);
}
foreach(源代码中的可变项)
{
DataRow行= output.NewRow();
foreach(属性中的var prop)
{
row [prop.Name] = prop.GetValue(item,null);
}
output.Rows.Add(row);
}
返回输出;
}
此更改解决了您的问题....
Rewrite your Linq Query this way
var SummingTotal = from b in cnmt
where b.CODE_NAME == SelectedItem
group b by b.CODE_NAME into g
select new
{
Branch_Name = g.Key,
Cnmt = g.Count(),
TWeight = g.Sum(twgt => Convert.ToInt32(twgt.AWGHT)),
CWeight = g.Sum(cwgt => Convert.ToInt32(cwgt.CWGHT)),
Freight = g.Sum(frt => frt.FREIGHT),
Risk = g.Sum(risk => risk.RC),
Cartridge = g.Sum(cartridge => cartridge.CARTRAGE),
Osc = g.Sum(osc => osc.OSC),
RC = g.Sum(rc => rc.RC),
Hamali = g.Sum(hamali => hamali.HAMALI),
Other = g.Sum(other => other.OTHER),
Insu = g.Sum(ins => ins.INSURANCE),
BC = g.Sum(bc => bc.BILTYCHARGE),
SC = g.Sum(sc => sc.SC),
PF = g.Sum(pf => pf.PF),
Claim = g.Sum(claim => claim.CLAIM),
Total = g.Sum(total => total.TOTAL),
Stax = g.Sum(stax => stax.STAX),
GTotal = g.Sum(Gtot => Gtot.GTOTAL),
};
GrvEnquiry.DataSource = SummingTotal.ToDataTable(); // call below function to conver linq query result into Data Table...
GrvEnquiry.DataBind();
// Than write following function to convert linq result to data table..
public static DataTable ToDataTable<t>(this IEnumerable<t> source)
{
PropertyInfo[] properties = typeof(T).GetProperties();
DataTable output = new DataTable();
foreach (var prop in properties)
{
output.Columns.Add(prop.Name, prop.PropertyType);
}
foreach (var item in source)
{
DataRow row = output.NewRow();
foreach (var prop in properties)
{
row[prop.Name] = prop.GetValue(item, null);
}
output.Rows.Add(row);
}
return output;
}
This changes solved your problem....
这篇关于如何将Linq结果集转换为数据表以绑定gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文