简体   繁体   English

根据 C# 中的某些条件管理列表中的项目

[英]managing items in a List based off some conditions in C#

I have a couple of lists List<OldData> oldItems = new List<OldData>();我有几个列表List<OldData> oldItems = new List<OldData>(); and List<NewData> newItems = new List<NewData>();List<NewData> newItems = new List<NewData>();

What I am trying to achieve here is to avoid the possibility of some redundant data coming in hence to check with some conditions before values are added into the list.我在这里试图实现的是避免一些冗余数据进入的可能性,因此在将值添加到列表中之前检查某些条件。

First up in the oldItems I will have some values stored and in the newItems list I will have a completely different set of values stored but it could always be the case that the newItems list may have some values that are already existing in the oldItems list.首先在oldItems 中,我将存储一些值,而在newItems列表中,我将存储一组完全不同的值,但可能总是这样, newItems列表可能具有一些已存在于oldItems列表中的值。

The two variables that are in use are a Name variable and a path variable which stores a certain path正在使用的两个变量是Name变量和存储某个路径的path变量

So First I want to check if there are any values where the name in the old items List is equal to the name in the new Items List , if There is an Item I want to check if the respective Path variable of the corresponding items are same ,所以首先我想检查是否有任何值,其中旧项目列表中的名称等于新项目列表中的名称,如果有一个项目,我想检查相应项目的相应 Path 变量是否相同,

if it is same I can simply keep that item the way it is in the OldItems list , if that happens to be different I will Remove that from the old Items List (and later use the item in the new Item List)如果相同,我可以简单地保持该项目在 OldItems 列表中的方式,如果恰好不同,我将从旧项目列表中删除它(然后在新项目列表中使用该项目)

So all the above in a nut shell ,综上所述,

I want to check for in the Old Items List where there is a Name Corresponding to it in the new Item List If there is , I want to check if the corresponding values in the Path variable for the two items with the same name from the two List are the same or different我想在旧项目列表中检查新项目列表中是否有与其对应的名称如果有,我想检查两个名称相同的两个项目在Path变量中的对应值列表相同或不同

I have made a simple dummy application to demo this but really stuck on how i can implement the logic我制作了一个简单的虚拟应用程序来演示这个,但真的坚持我如何实现逻辑

any suggestion ?有什么建议吗?

class Program
{
    static void Main(string[] args)
    {

        List<OldData> oldItems = new List<OldData>();

        OldData oldData1 = new OldData();
        oldData1.Name = "Test01";
        oldData1.OriginalPath = "D:/Temp01";

        oldItems.Add(oldData1);


        OldData oldData2 = new OldData();
        oldData1.Name = "Test02";
        oldData1.OriginalPath = "D:/Temp02";
        oldItems.Add(oldData2);


        List<NewData> newItems = new List<NewData>();

        NewData newData1 = new NewData();
        newData1.Name = "Test01";
        newData1.OriginalPath = "D:/Temp01";

        newItems.Add(newData1);

        NewData newData2 = new NewData();
        oldData1.Name = "Test05";
        oldData1.OriginalPath = "D:/Temp05";
        newItems.Add(newData2);

        foreach (var item in oldItems)
        {
            //Check if there are any Names which are same from the new Item list
            //If if there is such an item check if the OriginalPath variable is the same iin both
            //If yes => Remove that particular item from the Old data List 
            //If Not => Keep that Item in  the old data List
        }
    }
}

class OldData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
}

class NewData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
}

In this case since the value Test01 is common to both List It will check if the path variable values corresponding to both items are the same or not在这种情况下,由于值 Test01 对两个 List 是通用的,它会检查两个项目对应的路径变量值是否相同

Your dummy application is using the same object (oldData1).您的虚拟应用程序正在使用相同的对象 (oldData1)。

Please refer to the below code using simple LINQ.请使用简单的 LINQ 参考以下代码。

class Program
{
    static void Main(string[] args)
    {
        List<OldData> oldItems = new List<OldData>();

        OldData oldData1 = new OldData();
        oldData1.Name = "Test01";
        oldData1.OriginalPath = "D:/Temp01";

        oldItems.Add(oldData1);


        OldData oldData2 = new OldData();
        oldData2.Name = "Test02";
        oldData2.OriginalPath = "D:/Temp02";
        oldItems.Add(oldData2);


        List<NewData> newItems = new List<NewData>();

        NewData newData1 = new NewData();
        newData1.Name = "Test01";
        newData1.OriginalPath = "D:/Temp01";

        newItems.Add(newData1);

        NewData newData2 = new NewData();
        newData2.Name = "Test05";
        newData2.OriginalPath = "D:/Temp05";
        newItems.Add(newData2);


        oldItems = oldItems.Where(x => newItems != null && !newItems.Any(y => x.Name == y.Name && x.OriginalPath == y.OriginalPath)).ToList();

    }
}

class OldData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
}

class NewData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
}

1) I would implement an interface with the shared properties 1)我将实现一个具有共享属性的interface

2) We make both the classes implement it (which is already done anyway) 2)我们让两个类都实现它(无论如何已经完成了)

3) We implement an EqualityComparer for the specific interface which will check only the properties we want from both the objects implementing our interface 3) 我们为特定的interface实现了一个EqualityComparer ,它将只检查我们想要的两个实现我们interface的对象的属性

That's an implementation example:这是一个实现示例:

class Program
{
    static void Main(string[] args)
    {
        OldData oldData = new OldData
        {
            Name = "Pizza",
            OriginalPath = "Pozza"
        };

        NewData newData = new NewData
        {
            Name = "Pizza",
            OriginalPath = "Pozza"
        };

        var comparer = new CommonDataComparer();

        bool areEqual = comparer.Equals(oldData, newData);

        Console.WriteLine(areEqual);
    }
}

public interface ICommonData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
}

public class OldData : ICommonData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
    //Old other properties
}

public class NewData : ICommonData
{
    public string Name { get; set; }
    public string OriginalPath { get; set; }
    //New other properties
}

public class CommonDataComparer : IEqualityComparer<ICommonData>
{
    public bool Equals([AllowNull] ICommonData x, [AllowNull] ICommonData y)
    {
        return ((x?.Name == y?.Name) && (x?.OriginalPath == y?.OriginalPath));
    }

    public int GetHashCode([DisallowNull] ICommonData obj)
    {
        return obj.Name.GetHashCode() + obj.OriginalPath.GetHashCode();
    }
}

Output:输出:

True真的

You're comparing strings so the == operator will work fine.您正在比较字符串,因此==运算符将正常工作。

Here's a simple answer without using LINQ.这是一个不使用 LINQ 的简单答案。 Just a foreach loop with 2 conditions:只是一个有 2 个条件的 foreach 循环:

    for (int i = oldItems.Count - 1; i >= 0; i--)
    {
        OldData currentOldItem = oldItems[i] as OldData;
        foreach (var newItem in newItems) {
            if (currentOldItem.Name == newItem.Name) {
                if (currentOldItem.OriginalPath == newItem.OriginalPath) {
                    oldItems.RemoveAt(i);
                } 
            }
        }
    }

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

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