简体   繁体   English

根据项目更改字符串列表

[英]Alter a list of strings based on the items

I have a problem with a list that I want to alter, before outputting it back to the client.在将列表输出回客户端之前,我对要更改的列表有疑问。

For the sake of the question I will post an example of the list and how I need to result to look, because I have looked at Intersect, Except and everything else I could think of, but didn't get the result I am looking for.为了这个问题,我将发布一个列表示例以及我需要如何查看结果,因为我已经查看了 Intersect、Except 以及我能想到的所有其他内容,但没有得到我正在寻找的结果.

Example List:示例列表:

1, 4, 6, 8 1、4、6、8
1, 2, 6, 8 1、2、6、8
2, 4, 6, 8 2, 4, 6, 8
3, 4, 5, 7 3、4、5、7

Required Result:要求的结果:

1, 4, 6, 8 //Initial row 1, 4, 6, 8 //第一行
-, 2, -, - //Items that have not changed will show as a - -, 2, -, - //未更改的项目将显示为 -
2, 4, -, - 2, 4, -, -
3, -, 5, 7 3, -, 5, 7

I really hope I explained it well.我真的希望我解释得很好。

I would be happy to explain this further if needed.如果需要,我很乐意进一步解释这一点。

Thanks in advance for the advice, so far I have wrecked my brain over this.在此先感谢您的建议,到目前为止,我已经为此伤透了脑筋。 ;) ;)

What I tried is too much to type here, so here is what I have so far.我尝试的太多了,无法在这里输入,所以这是我目前所拥有的。 Except simply won't do anything with the data because it thinks the rows are different, so they just stay the same.除了不会对数据做任何事情,因为它认为行是不同的,所以它们只是保持不变。

private List<List<string>> FilterData(List<string[]> datatable)
    {
        List<string> previousRow = new List<string>();
        List<string> currentRow = new List<string>();
        List<string> rowDifferences = new List<string>();

        List<List<string>> resultingDataset = new List<List<string>>();

        foreach (var item in datatable)
        {
            if (previousRow == null)
            {
                previousRow = item.ToList();
                continue;
            }

            currentRow = item.ToList();

            rowDifferences = currentRow.Except(previousRow).ToList();
            resultingDataset.Add(rowDifferences);
        }
        return resultingDataset;
    }
private static List<List<string>> FilterData(List<List<string>> datatable)
{
    var result = new List<List<string>>();

    for(var rowindex = 0; rowindex < datatable.Count; rowindex++)
    {
        // Clone the string list
        var refrow = datatable[rowindex]
            .Select(item => (string)item.Clone()).ToList(); 

        result.Add(refrow);

        // First row will not get modify anyway
        if (rowindex == 0) continue;

        var row = result[rowindex];
        // previous row of result has changed to "-", so use the original row to compare
        var prevrow = datatable[rowindex - 1]; 
        for(var columnindex = 0; columnindex < row.Count; columnindex++)
        {
            if (row[columnindex] == prevrow[columnindex])
                row[columnindex] = "-";
        }
    }

    return result;
}

fiddle小提琴

public static List<List<T>> RemoveDuplicates<T>(this List<List<T>> items, T replacedValue) where T: class
{
    List<List<T>> ret = items;

    items.ForEach(m=> {
        var ind = items.IndexOf(m);

        if(ind==0)
        {
            ret.Add(items.FirstOrDefault());
        }
        else
        {
            var prevItem = items.Skip(items.IndexOf(m)-1).FirstOrDefault();

            var item = new List<T>();
            for(var a = 0; a < prevItem.Count; a++)
            {
                item.Add(prevItem[a] == m[a]? replacedValue : m[a]);
            }

            ret.Add(item);
        }
    });

    return ret;
}

How to use it:如何使用它:

var items = new List<List<string>>{
    new List<string>{ "1", "4", "6", "8" },
    new List<string>{ "1", "2", "6", "8" },
    new List<string>{ "2", "4", "6", "8" },
    new List<string>{ "3", "4", "5", "7" }
};

var result = items.RemoveDuplicates("-");

dotNetFiddle: https://dotnetfiddle.net/n36p64 dotNetFiddle: https ://dotnetfiddle.net/n36p64

Few things you have to change in your code;您必须在代码中更改一些内容;

Here is code:这是代码:

 private List<string[]> FilterData(List<string[]> datatable)
    {
        // List is made of String Array, so need string[] variable not list
        string[] previousRow = null ;
        string[] currentRow;
        string[] rowDifferences ;

        // to store the result
        List<string[]> resultingDataset = new List<string[]>();

        foreach (var item in datatable)
        {
            if (previousRow == null)
            {
                previousRow = item;
                resultingDataset.Add(previousRow); // add first item to list
                continue;
            }

            currentRow = item; 

            // check and replace with "-" if elment exist in previous 
            rowDifferences = currentRow.Select((x, i) => currentRow[i] == previousRow[i] ? "-" : currentRow[i]).ToArray();  
            resultingDataset.Add(rowDifferences);

            // make current as previos
            previousRow = item;
        }
        return resultingDataset;
    }

check this dotnetfiddle检查这个dotnetfiddle

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

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