查找值是否存在于 dataset.Table(第 0 列)中。如果是,获取行索引并更新(第 1 列)中的值

Find if value exists in dataset.Table (column 0). if so, get row index and update value in (column 1)

我真的找不到我的问题的答案,我真的不知道该写些什么来找到相关的东西。

我得到了一个布尔值数组。我想检查每个数字是否在我的数据集中。如果是这样我想检查它的布尔值是否为真,如果值为假我想更新那个值。

 foreach (var item in EU)
        {
            if (objDataSet.Tables[0].AsEnumerable().Any(roww => Convert.ToInt64(item.Substring(1, item.Length - 1)) == roww.Field<Int64>(0)))
            {
                if (objDataSet.Tables[0].AsEnumerable().Any(rowa => true == rowa.Field<bool>(1)))
                {
                    ExistingPhones.Add(item.Substring(1, item.Length - 1), true);
                }
                else
                {
                    UpdatePhones.Add(item.Substring(1, item.Length - 1), true);
                }
            }
            else
            {
                ActivePhones.Add(item.Substring(1, item.Length - 1), true);
            }
        }

我已经解决了所有问题,但是第二个 If 语句非常慢并且运行时间成倍增加。在第一个if语句中找到数字后,如何直接检查它的布尔值?

P.D: Don´t mind the ".Substring(1, item.Length -1)" that´s because I receive numbers with a plus -> +34666999333 and I need to erase it to store it into the database as a BigInt

也许在这种情况下使用两次 Linq 调用 AsEnumerable 和 Any 并不比使用 DataTable 对象的旧 Select 方法更好。在您的代码中,您执行了两次提到的模式,也许这不是 'performance wise'.
您可以使用 DataTable.Select 方法避免它并将结果存储在 DataRow 数组中。

foreach (var item in EU)
{
    string phoneWithoutPlus = item.Substring(1, item.Length - 1);
    var rows = objDataSet.Tables[0].Select("Number = " + phoneWithoutPlus);
    if (rows.Length > 0 && rows[0].Field<bool>(1) == true)
    {
        ExistingPhones.Add(phoneWithoutPlus, true);
    }
    else if (rows.Length > 0 && rows[0].Field<bool>(1) == false)
    {
        UpdatePhones.Add(phoneWithoutPlus, true);
    }
    else
    {
        ActivePhones.Add(phoneWithoutPlus, true);
    }
}

不要使用 Any(),而是使用 FirstOrDefault() 获取对象并将其保存到变量中:

var myObj = objDataSet.Tables[0].AsEnumerable()
.Where(roww => Convert.ToInt64(item.Substring(1, item.Length - 1)) == roww.Field<Int64>(0))
.FirstOrDefault();

if (myObj != null)
{
    //reuse myObj here
    if (myObj...)
    {
        ExistingPhones.Add(item.Substring(1, item.Length - 1), true);
    }
    else
    {
        UpdatePhones.Add(item.Substring(1, item.Length - 1), true);
    }
}
else
{
    ActivePhones.Add(item.Substring(1, item.Length - 1), true);
}

你为什么不创建一个字典。字典在键和数据表的行之间创建一个link而不复制数据表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet objDataSet = new DataSet();

            Dictionary<Int64, DataRow> dict = objDataSet.Tables[0].AsEnumerable()
                .GroupBy(x => x.Field<Int64>(0), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            foreach (var item in EU)
            {
                Int64 itemKey = Convert.ToInt64(item.Substring(1, item.Length - 1));
                if (dict.ContainsKey(itemKey))
                {
                    DataRow row = dict[itemKey];
                    if (row.Field<bool>(1) == true)
                    {
                        ExistingPhones.Add(phoneWithoutPlus, true);
                    }
                    else
                    {
                        UpdatePhones.Add(phoneWithoutPlus, true);
                    }

                }
                else
                {
                    ActivePhones.Add(itemKey, true);
                }
            }
        }
    }
}

​