如何在C#中比较两个数据表 [英] How to compare two datatable in C#
问题描述
我有两个来自excel的数据表,这里我想从第一个数据表中搜索数据并检查第二个表中的可用性,如果可用,那么想在第一个数据表中添加一个新列,并希望将值设置为现在
我尝试过:
我创建了两个数据表并尝试使用Linq但不知道如何实现它
I am having two datatable from excel ,Here I want to search the data from first datattable and check for the availability in second table ,If it available then want to add a new column in first datatable and want to set the value as present
What I have tried:
I have created two datatable and tried using Linq but don't know how to implement it
推荐答案
我们无法为您提供解决方案:它主要取决于数据,以及列组织。
首先查看两个数据源:确定需要使用哪些列以及哪些列定义可用性,并确切地说明如何判断一行是可用的。当你知道的时候,你可以开始寻找它们了。
实际上这样做非常简单:编写一个接受行的方法并检查它的可用性 - 然后,您可以使用Linq(如果您愿意)处理Table1中的所有行并构建可用的行集合。
We can't give you a solution to this: it depends on the data primarily, and the column organisation.
Start by looking at your two data sources: identify which columns you need to work with and which define "availability", and work out exactly how you tell a row is "available". When you know that, you can start looking at finding them.
Actually doing it is pretty easy: write a method that accepts a row and checks it for "availability" - you can then use Linq (if you want to) to process all the rows from Table1 and build a collection of rows which are available.
var available = myDataTable.AsEnumerable().Select(r => IsAvailable(r));
但是......在表1中添加一个新列有点奇怪 - 你不想为每个可用行这样做,或者你呢?我建议你需要仔细考虑一下你想要达到的目标
But ... "add a new column in table 1" is a bit odd - you don't want to do that for every "available row", or do you? I'd suggest that you need to think a bit more carefully about exactly what you are trying to achievedatatable
这篇关于如何在C#中比较两个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!