C# 用于将多个字符串部分列表与主字符串部分列表进行比较的模式

C# 用于将多个字符串部分列表与主字符串部分列表进行比较的模式,c#,list,design-patterns,comparison,C#,List,Design Patterns,Comparison,所以我有一个“优步”列表:DestinationColumn,有100个字符串。比如说,为了简单起见,总是100 它来自几个来源栏目,可乐,可乐。其中一些将有100个字符串,一些将有更少的字符串 例如: ColA具有字符串1-33、40-70和90-100的值。它获得第一优先级,并将这些字符串的所有值填充到DestinationColumn中。 ColB的值为1-40和70-100。如果存在间隙,它会将其填满,但不能使线过度疲劳 现在我要确保这一切都发生了 列出sourceColA,列出sour

所以我有一个“优步”列表:DestinationColumn,有100个字符串。比如说,为了简单起见,总是100

它来自几个来源栏目,可乐,可乐。其中一些将有100个字符串,一些将有更少的字符串

例如:

ColA具有字符串1-33、40-70和90-100的值。它获得第一优先级,并将这些字符串的所有值填充到DestinationColumn中。 ColB的值为1-40和70-100。如果存在间隙,它会将其填满,但不能使线过度疲劳

现在我要确保这一切都发生了

列出sourceColA,列出sourceColB 和ListdestinationColumn

首先,我编写了一个函数来比较两个列表,看看它们是否包含相同的字符串:

public static bool ListEquals<T>(IList<T> list1, IList<T> list2)
    {
        if ((list1 == null) || (list2 == null))
        {
            return false;
        }
        if (list1.Count != list2.Count)
            return false;
        for (int i = list1.Count - 1; i >= 0; --i)
        {
            if (!list1[i].Equals(list2[i]))
            {
                return false;
            }
        }
        return true;
    }
公共静态bool listquals(IList列表1、IList列表2)
{
if((list1==null)| |(list2==null))
{
返回false;
}
if(list1.Count!=list2.Count)
返回false;
for(int i=list1.Count-1;i>=0;--i)
{
如果(!list1[i].Equals(list2[i]))
{
返回false;
}
}
返回true;
}
现在,我想保持它的通用性,但是使用x个源列,并确保所有内容现在都正确地位于一个目标列中。有这样做的一般模式吗


我目前的想法是一个循环,将所有的ColA条目与destinationCol进行标记,重复B,以此类推,然后确保所有条目不会被击中两次,并且所有条目都被击中一次

要合并您描述的两列,您只需

ColA.Zip(ColB, (a, b) => a??b)
因此:


我不能完全确定我是否理解你想做什么,因为我不理解你的ListQuals的目的。不管怎样,我会这样做:

    public static void FillDest<T>(IList<T> dest, IList<IList<T>> srcLists)
        where T : class
    {
        bool goon = true;
        int idx = 0;
        dest.Clear();
        while (goon)
        {
            goon = false;
            bool added = true;
            foreach (IList<T> aSrc in srcLists)
            {
                added = false;
                T anItem;
                if (aSrc.Count <= idx)
                {
                    added = true;
                    continue;
                }
                goon = true;
                if ((anItem = aSrc[idx]) != null)
                {
                    dest.Add(anItem);
                    added = true;
                    break;
                }
            }
            if (!added)
                dest.Add((T)null);
            idx++;
        }
    }
publicstaticvoidfilldest(IListdest,IListsrclists)
T:在哪里上课
{
bool-goon=true;
int-idx=0;
目标清除();
while(goon)
{
傻瓜=假;
bool added=true;
foreach(srcLists中的IList aSrc)
{
添加=错误;
电子显微镜;

if(aSrc.Count)回答得不错,但是
var maxItems
行非常昂贵,并且可能有在迭代之间更改值的“热”枚举。将
cols
推到数组中并从第0列开始作为初始聚合累加器可能是值得的-然后
Zip
可以正常工作。
static class Ex
{
    public static IEnumerable<T> MergeLeft<T>
        (this IEnumerable<IEnumerable<T>> cols) where T:class
    {
        return cols
            .Aggregate(
                Enumerable.Empty<T>(),
                (acc,col) => 
                acc
                    .Concat(Infinite<T>(null))
                    .Zip(col, (a, b) => a ?? b));   
    }
    private static IEnumerable<T> Infinite<T>(T item)
    {
        for(;;){yield return item;}
    }
}
    public static void FillDest<T>(IList<T> dest, IList<IList<T>> srcLists)
        where T : class
    {
        bool goon = true;
        int idx = 0;
        dest.Clear();
        while (goon)
        {
            goon = false;
            bool added = true;
            foreach (IList<T> aSrc in srcLists)
            {
                added = false;
                T anItem;
                if (aSrc.Count <= idx)
                {
                    added = true;
                    continue;
                }
                goon = true;
                if ((anItem = aSrc[idx]) != null)
                {
                    dest.Add(anItem);
                    added = true;
                    break;
                }
            }
            if (!added)
                dest.Add((T)null);
            idx++;
        }
    }