通过Linq比较两个数据 [英] Comparing two datatables through Linq

查看:255
本文介绍了通过Linq比较两个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Linq比较两个 datatable 。但是我得到这个例外:

Hi I'm trying to compare two datatable through Linq. But I get this exception:


特定转换无效

Specific cast is invalid

请帮助我,因为我是Linq的新手。这是我正在使用的代码:

Please help me as I am new to Linq. This is the code I'm using:

var matched1 = from table1 in dtAvailableStores.AsEnumerable()
               join table2 in dtControlStores.AsEnumerable() 
               on table1.Field<int>("STORE_NBR") 
               equals table2.Field<int>("STORE_NBR")
               select table1;

这里 STORE_NBR 是一个字符串值。 / p>

Here STORE_NBR is a string value.

推荐答案

您可以使用这段代码有一个相当不错的主意:

You can have a fairly good idea with this piece of code:

var qry1 = datatable1.AsEnumerable().Select(a => new { MobileNo = a["ID"].ToString() });
var qry2 = datatable2.AsEnumerable().Select(b => new { MobileNo = b["ID"].ToString() });

var exceptAB = qry1.Except(qry2);

DataTable dtMisMatch = (from a in datatable1.AsEnumerable() join ab in exceptAB on a["ID"].ToString() equals ab.MobileNo select a).CopyToDataTable();

参考文献:


  1. 使用LINQ查询比较两个datatable

  2. 比较两个DataTable的差异在C# ?

  1. Compare two datatable using LINQ Query
  2. Compare two DataTables for differences in C#?

这篇关于通过Linq比较两个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆