【问题标题】:How to compare two lists based on indexes如何根据索引比较两个列表
【发布时间】:2022-01-03 14:45:34
【问题描述】:

我有两个长度相同的列表。 如果一个列表有 4 个元素,则另一个列表也有 4 个元素。 List<string> multipleJMBGsList<BundleSchedule> schedules

我需要创建一个检查方法,它将检查以下内容:

  • 首先检查List<string> 中是否有重复项,如果有,则从该查询中获取索引,并在这些索引中检查调度是否具有相同的 ID if schedules[x].Id == chedules[y].Id

  • 可以有多个相同的对,例如:

"1111", "1111" (indexes [23],[41])
"12345", "12345" (indexes [3],[11])
"16872982342716", "16872982342716" (indexes [29],[33])

这些是 3 对,所以我们需要分组,并提取它们的索引(这些数字仅用于示例目的):

private bool CheckIfSameUsersHaveSameServices(List<string> multipleJMBGs, List<BundleSchedule> schedules)
{
    var duplicateJMBGs = multipleJMBGs.GroupBy(x => x)
                .Where(group => group.Count() > 1)
                .Select(group => new { jmbg = group.Key }).ToList();


           
    Dictionary<string, string> indexes = new Dictionary<string, string>();

    //fill in dictionary with indexes
    //23,41
    //3,11
    //29,33

    foreach (var pair in indexes)
    {
        var firstToCompare = schedules.ElementAt(Convert.ToInt32(pair.Key));
        var secondToCompare = schedules.ElementAt(Convert.ToInt32(pair.Value));

        //if only one compared pair has same serviceId, return true
        if (firstToCompare.ServiceTypeComplexityId == secondToCompare.ServiceTypeComplexityId)
        {
            return true;
        }
    }
}

我的问题是如何将 GroupBy 查询的 Select 也放入列表中的那些索引?

【问题讨论】:

    标签: c# .net asp.net-mvc linq


    【解决方案1】:

    怎么样:

    Dictionary<string, int> jmbgIds = new Dictionary<string, int>(StringComparer.Ordinal);
    
    for (int index = 0; index < multipleJMBGs.Count; index++)
    {
        string jmbg = multipleJMBGs[index];
        int id = schedules[index].ServiceTypeComplexityId;
        if (jmbgIds.TryGetValue(jmbg, out var previousId))
        {
            if (previousId != id)
            {
                return false;
            }
        }
        else
        {
            jmbgIds.Add(jmbg, id);
        }
    }
    
    return true;
    

    【讨论】:

    • 谢谢,它有效,只是我稍微修改了previousId == id =&gt; return true nad 底部返回 false
    • @Stefan0309 从问题和方法名称中我不能完全确定您是否想确定 所有重复项(如果有)是否具有相同的 ID那里至少有一个具有相同 ID 的重复项。 :)
    【解决方案2】:

    您可以获取每个可枚举的索引和值并执行联接。 小提琴:https://dotnetfiddle.net/0oDJMM

    public static void Main()
    {
        List<string> foo = new List<string>() {
            "1", "12", "123", "1234", "12345"
        };
        List<string> bar = new List<string>() {
            "123", "a", "b", "1", "12"
        };
        var foos = foo.Select((f, i) => new { idx = i, val = f });
        var bars = bar.Select((b, i) => new { idx = i, val = b });
        var indexes = foos.Join(bars, 
            f => f.val, 
            b => b.val, 
            (f, b) => new { idxA = f.idx, idxB = b.idx });
        
        foreach (var idxs in indexes) {
            Console.WriteLine("idxA: {0} idxB: {1}", idxs.idxA, idxs.idxB); // You can now access the indexes for the matching values
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      相关资源
      最近更新 更多