简体   繁体   English

我必须比较两个数据表c#asp.net,比较它的两列,并将结果列添加到新的数据表中

[英]i have to compare two datatables c# asp.net, compare its two columns and add that resultant column in the new datatable

I have tried this , but i don't know how to manipulate LINQ query to find difference between two columns from the data table. 我已经尝试过了,但是我不知道如何操纵LINQ查询来查找数据表中两列之间的差异。

I have two datatables dtStockTransactionData and dtStocksData . 我有两个数据表dtStockTransactionDatadtStocksData Below code explains how I have manupulated the result 下面的代码说明了我如何处理结果

DataTable dtMerged = (from a in dtStockTransactionData.AsEnumerable()
                      join b in dtStocksData.AsEnumerable()
                      on a["exchangesymbol"].ToString() equals b["exchange_symbol"].ToString()
                      into g
                      where g.Count() > 0
                      select a).CopyToDataTable();
grdErrSTSData.DataSource = dtMerged;
grdErrSTSData.DataBind();
return dtMerged;

If I understood your question you can do this using link query with select new keyword.This is a example for Link with join - 如果我理解了您的问题,则可以使用带有select new关键字的链接查询来完成此操作。这是带有连接的链接的示例-

from a in db.table1
   join b in db.table2 on a.valueA equals b.valueB
   select new { A = a.valueA, B = valueB};

As your code: 作为您的代码:

 var selected = from a in dtStockTransactionData.AsEnumerable()
                      join b in dtStocksData.AsEnumerable()
                      on a["exchangesymbol"].ToString() equals b["exchange_symbol"].ToString()
                      into g
                      where g.Count() > 0
                      select new{ 
                         value1 = a.wantedValue,
                         value2 = b.wantedValue
                      }

Then you can get result using selected 然后您可以使用selected结果

After that you can compare data in own way. 之后,您可以按照自己的方式比较数据。

Refer this Return list using select new in LINQ 使用LINQ中的select new引用此返回列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM