Linq query to search for various combinations of list items taking 2 at a time C #

var com = from exam1 in timeTable.Exams
          from exam2 in timeTable.Exams
          where (exam1 != exam2)
          select new List<Exam> { exam1, exam2 };

timeTable is a list of exams

How can I make com be great no matter what exam order it contains. I think that now he performs permutations, I need only different combinations.

+3
source share
5 answers
var exams = timeTable.Exams.ToList();

var com = exams.Select(x => exams.Where(y => exams.IndexOf(y) > exams.IndexOf(x))
    .Select(z => new List<Exam> {x, z}))
    .SelectMany(x => x);
+4
source

If you can implement a comparison such as IComparable<Exam>(which you can, for example, compare based on a key field such as ExamId), you could do it as shown below (showing an example using integers)

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

var query = from item1 in array
            from item2 in array
            where (item1.CompareTo(item2) < 0)
            select new Tuple<int, int>(item1, item2);

foreach (Tuple<int, int> tuple in query)
    Console.WriteLine("{0}\t{1}", tuple.Item1, tuple.Item2);

Func<> inline ​​, , .

Func<int, int, int> itemComparer = (x, y) => x.CompareTo(y);
// in your case, perhaps 
// Func<Exam, Exam, int> examComparer = (exam1, exam2) => exam1.Id.CompareTo(exam2.Id);

var query = from item1 in array
            from item2 in array
            where itemComparer(item1, item2) < 0
            select new Tuple<int, int>(item1, item2);

item2, , item1.

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

var query = from item1 in array.Select((item, index) => new { item, index })
            from item2 in array.Select((item, index) => new { item, index })
            where (item1.index < item2.index)
            select new Tuple<int, int>(item1.item, item2.item);

foreach (Tuple<int, int> tuple in query)
    Console.WriteLine("{0}\t{1}", tuple.Item1, tuple.Item2);
+2

, :

class UnorderedTuple<T1, T2> {
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public UnorderedTuple(T1 item1, T2 item2) {
        this.Item1 = item1;
        this.Item2 = item2;
    }

    public override bool Equals(object obj) {
        if (Object.ReferenceEquals(this, obj)) {
            return true;
        }
        UnorderedTuple<T1, T2> instance = obj as UnorderedTuple<T1, T2>;
        if (instance == null) {
            return false;
        }
        return (this.Item1.Equals(instance.Item1) &&
                this.Item2.Equals(instance.Item2)) ||
               (this.Item1.Equals(instance.Item2) &&
                this.Item2.Equals(instance.Item1)
               );
    }

    public override int GetHashCode() {
        return this.Item1.GetHashCode() ^ this.Item2.GetHashCode();
    }
}

, :

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

var pairs = from item1 in array
            from item2 in array
            where item1 != item2
            select new UnorderedTuple<int, int>(item1, item2);

var distinct = pairs.Distinct();
foreach (var item in distinct) {
    Console.WriteLine("{{{0}, {1}}}", item.Item1, item.Item2);
}

{1, 2}. ,

Console.WriteLine(distinct.Count());

36, , 9 * 8 / 2 = 72 / 2 = 36 (9 * 8 , 2, ).

:

var com = (from exam1 in timeTable.Exams
           from exam2 in timeTable.Exams
           where (exam1 != exam2)
           select new UnorderedTuple<Exam, Exam>(exam1, exam2)
          ).Distinct();
0

:

var com = exams
          .Select(i => new {Head = new [] {i}, Tail = exams.SkipWhile(j => j != i).Skip(1)})
          .Select(l => l.Head.Join(l.Tail, x => 0, x => 0, (a,b) => new {a,b}))
          .SelectMany(l => l);

: ( 1,2,3,4)
1: . , head.
(: {[1], [2,3,4]}, {[2], [3,4] }, {[3], [4]}, {[], []})
2: .
(: [[1,2], [1,3], [1,4]], [[2,3 ], [2,4]], [[3,4]])
3:

0

- ?

var array = timeTable.Exams.ToArray();
var twoTuples = from index1 in Enumerable.Range(0, array.Length)
                from index2 in Enumerable.Range(i, array.Length - i)
                select Tuple.Create(array[index1], array[index2]);

(, List<Exam > , , , ).

(x, x), where index1 != index2 , , .

, "" ( ), .Distinct() . , Tuple<,> Equals GetHashCode.

< > :

  • - .
  • .

for . , , LINQy.

0

Source: https://habr.com/ru/post/1785239/


All Articles