开发者

How to get moving combination from two List<String> in C#?

I'm having two List<String> which contains

ListOne
          A
          B
          C
ListTwo
          A
          B
          C
          D

Now i need to get the moving combinations to a list string

So the output list will contain

         A-B
         A-C
         A-D
         B-C
         B-D
         C-D

Now i'm using Nested for loop for this.? Is there any way to do this using LINQ or LAMBDA EXPRESSION Please help me to do this. Thanks in advance

Sample Code

List<String> ListOne = new List<string> { "A","B","C"};
List<String> ListTwo = new List<string> { "A", "B", "C", "D" };

List<String> Result = new List<string>(from X in ListOne 
                                       from Y in ListTwo 
                                       where X!=Y
                                        select string.Format("{0}-{1}", X, Y));

But its not giving the correct output

  It produces like

            A-B
            A-C
            A-D
            B-A
            B-C
            B-D
            C-A
            C-B
            C-D

But the required output is like

         A-B
         A-C
         A-D
         B-C
         B-D
         C-D

开发者_高级运维Sample Code using For Loop

List<String> ResultTwo = new List<string>();
        for (int i = 0; i < ListOne.Count; i++)
        {
            for (int j = 0; j < ListTwo.Count; j++)
            {
                if(ListOne[i] != ListTwo[j])
                    if (ResultTwo.Contains(ListOne[i] + "-" + ListTwo[j]) == false && ResultTwo.Contains(ListTwo[j] + "-" + ListOne[i]) == false) 
                ResultTwo.Add(ListOne[i] + "-" + ListTwo[j]);
            }
        }

its working fine.... but i just need a simple way ( Using LINQ)


Following your edits this should do the trick:

List<string> ListOne = new List<string>(){"A","B","C"};
List<string> ListTwo = new List<string>(){ "A","B","C","D"};

var result = from a in ListOne
             from b in ListTwo
             let condition = a.CompareTo(b)
             where condition != 0
             select condition < 0 ? a + "-" + b : b + "-" + a;


foreach (var v in result.Distinct())
{
    Console.WriteLine(v);
}

The second version that preserves order (ItemFromList1 - ItemFromList2):

        var result =
            from o in
                (
                    from a in ListOne
                    from b in ListTwo
                    let condition = a.CompareTo(b)
                    where condition != 0
                    select new { a, b, condition }
                )
                group o by
                    o.condition < 0 ? o.a + "-" + o.b : o.b + "-" + o.a into g
                select g.Select(n => n.a + "-" + n.b).Take(1).ToArray()[0];


So you want all matches except

  • item matched to itself
  • item matched to smaller item

and no duplicates.

Here it is:

IEnumerable<string> result = 
  (
    from a in ListOne
    from b in ListTwo
    where a != b
    select a.CompareTo(b) < 0 ? a + "-" + b : b + "-" + a
  ).Distinct();

Stating the requirement is 90% of the battle.


If you NEED the first item in the pairing to come from the first list, this will do it:

IEnumerable<string> result = 
  (
    from a in ListOne
    from b in ListTwo
    select new
    {
      A = a,
      B = b,
      Combo = a.CompareTo(b) < 0 ? a + "-" + b : b + "-" + a
    } into x
    group x by x.Combo into g
    select g.Select(x2 => x2.A + "-" + x2.B).First()
  )


var listA = new List<string> { "A", "B", "C" };
var listB = new List<string> { "A", "B" };
var result = listA.SelectMany((a, indexA) =>
                     listB.Where((b, indexB) => 
                            listB.Contains(a) ? !b.Equals(a)&&indexB > indexA 
                                              : !b.Equals(a))
                          .Select(b => string.Format("{0}-{1}", a, b)));


How about the logic of validating is added to the list

class Program
{
    static void Main(string[] args)
    {
        List<string> a = new List<string>() { "C", "D", "L" };
        List<string> b = new List<string>() { "C", "L", "C", "D" };

        var pairValuesNotEqual = from vara in a
                from varb in b
                where vara != varb
                select new Pair(vara, varb);


        Set sets = new Set();
        sets.AddRange(pairValuesNotEqual);

        foreach (var item in sets)
        {
            Console.WriteLine(item.First + " - " + item.Second);
        }

        Console.ReadLine();
    }
}

public class Set : List<Pair>
{
    public new void AddRange(IEnumerable<Pair> pairs)
    {
        foreach (var item in pairs)
        {
            this.Add(item);
        }
    }

    public new void Add(Pair item)
    {
        if (!IsExists(item))
            base.Add(item);
    }

    private bool IsExists(Pair item)
    {
        foreach (Pair i in this)
        {
            if (i.First == item.Second && i.Second == item.First)
                return true;
        }

        return false;
    }
}


I dont think LINQ is good enough for this, I got this, but it is not good looking :

        var A = from one in ListOne
                from two in ListTwo
                where one != two
                let x = one.CompareTo(two) < 0 ? one : two
                let y = one.CompareTo(two) < 0 ? two : one
                select new { X = x, Y = y};

        var B = A.Distinct().Select(a => a.X + "-" + a.Y);

Acualy, when I clean up your nested foreach code a little bit, I like it more than LINQ:

        List<string> outList = new List<string>();
        foreach (string s1 in ListOne)
        {
            foreach (string s2 in ListTwo)
            {
                if (s1 != s2 &&
                    !outList.Contains(s1 + "-" + s2) &&
                    !outList.Contains(s2 + "-" + s1))
                {
                    outList.Add(s1 + "-" + s2);
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜