简体   繁体   English

将数据表中两列的值合并到一个列表中

[英]Combine values from two columns in a data table to a single list

I am trying to build a list of integers from a data table by combining the data in two of the columns in the data table and getting a distinct list of values.我正在尝试通过组合数据表中两列中的数据并获取不同的值列表来从数据表中构建整数列表。

To get the data table.. i am calling a stored procedure (which i don't have the ability to edit)获取数据表..我正在调用一个存储过程(我没有能力编辑)

After I call my method to return the data table that's returned by the stored procedure.. I want to look at two of the columns in the data table and get a list of values that are in these columns.在我调用我的方法返回存储过程返回的数据表之后。我想查看数据表中的两列并获取这些列中的值列表。

The data table returned by the stored procedure looks like this:存储过程返回的数据表如下所示:

在此处输入图像描述

I want to get a list of the values in columns MailerKey and BillToKey.我想获取 MailerKey 和 BillToKey 列中的值列表。
What is the best way to do this?做这个的最好方式是什么? Can I use Linq to do this?我可以使用 Linq 来执行此操作吗?

So far I've tried doing:到目前为止,我已经尝试过:

    using(DataTable dt = getCustomers())
    {
        List<int> MailerKeys = dt.AsEnumerable().Select(x => x.Field<int>("MailerKey")).Distinct().ToList();
        List<int> BillToKeys = dt.AsEnumerable().Select(x => x.Field<int>("BillToKey")).Distinct().ToList();
    }

But how can I now combine the values?但是我现在如何组合这些值?

By combine I mean, to get a list that contains the distinct values from both columns.. so eg: I should get back:结合我的意思是,要获得一个包含两列不同值的列表..所以例如:我应该回来:

275
58
250
50
59
99
55

You could:你可以:

  • create a HashSet<int> and add both lists to it;创建一个HashSet<int>并向其中添加两个列表;
  • add one list to the other and use Distinct from System.Linq .将一个列表添加到另一个列表并使用Distinct from System.Linq

For example:例如:

var a = new[] { 1, 2 }; // Could be any IEnumerale<int>
var b = new[] { 2, 3 }; 

var hash = new HashSet<int>(a);
hash.UnionWith(b);

Console.WriteLine(string.Join(",", hash)); // Prints 1,2,3

You can use Linq to achieve the "combine" step.您可以使用 Linq 来实现“组合”步骤。 First concatenate your lists using Concat, then keep only unique values using Distinct.首先使用 Concat 连接您的列表,然后使用 Distinct 仅保留唯一值。

using(DataTable dt = getCustomers())
{
    var mailerKeys = dt.AsEnumerable().Select(x => x.Field<int>("MailerKey")).Distinct().ToArray();
    var billToKeys = dt.AsEnumerable().Select(x => x.Field<int>("BillToKey")).Distinct().ToArray();

    var combinedDistinctKeys = mailerKeys.Concat(billToKeys).Distinct();
}

Notes:笔记:

  • 'combinedDistinctKeys' is not yet enumerated, this would occur using ToArray or ToList for example; 'combinedDistinctKeys' 尚未枚举,例如使用 ToArray 或 ToList 会发生这种情况;
  • I preferred to use ToArray for the source lists, since I know I will not need List<> features.我更喜欢将 ToArray 用于源列表,因为我知道我不需要 List<> 功能。

For more information about the Concat method from Linq, see: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.concat?view=net-5.0有关 Linq 中的 Concat 方法的详细信息,请参阅: https://docs.microsoft.com/en-us/dotnet/api/system.Z83C8324F192E757DE4EA9C519D5DB62=CZ5erableview.concat?

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

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