根据项目更改字符串列表

时间:2015-09-10 05:56:09

标签: c# list list-manipulation

我将要更改的列表出现问题,然后再将其输出回客户端。

为了这个问题,我将发布一个列表的例子以及我需要如何看待,因为我已经看过Intersect,Except和我能想到的其他一切,但没有得到结果我正在寻找。

示例列表:

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

必填结果:

1,4,6,8 //初始行
- ,2, - , - //未更改的项目将显示为 -
2,4, - , -
3, - ,5,7 <

我真的希望我解释得很好。

如果需要,我很乐意进一步解释。

提前感谢您的建议,到目前为止,我已经破坏了我的大脑。 ;)

我在这里输入的内容太多了,所以这就是我到目前为止所做的。除了简单地不对数据做任何事情,因为它认为行是不同的,所以它们保持不变。

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;
    }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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;
}

如何使用它:

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

答案 2 :(得分:0)

您的代码中必须更改的内容很少;

这是代码:

 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;
    }

检查此dotnetfiddle