C# -B。但是预期的输出是A-B,C-A,C-B@Pramodh,您如何获得该逻辑?@astander:请参阅我的第二次编辑(使用For Loop的示例代码)@欣快我认为你的否决票是不合理的,因为原始问题和概念被编辑了3次,我没有跟踪所有的更改。第二个显示了一
C# -B。但是预期的输出是A-B,C-A,C-B@Pramodh,您如何获得该逻辑?@astander:请参阅我的第二次编辑(使用For Loop的示例代码)@欣快我认为你的否决票是不合理的,因为原始问题和概念被编辑了3次,我没有跟踪所有的更改。第二个显示了一,c#,linq,lambda,C#,Linq,Lambda,-B。但是预期的输出是A-B,C-A,C-B@Pramodh,您如何获得该逻辑?@astander:请参阅我的第二次编辑(使用For Loop的示例代码)@欣快我认为你的否决票是不合理的,因为原始问题和概念被编辑了3次,我没有跟踪所有的更改。第二个显示了一些错误,如“无法隐式转换类型”字符串“为”bool”` ListOne A B C ListTwo A B C
-B。但是预期的输出是
A-B
,C-A
,C-B
@Pramodh,您如何获得该逻辑?@astander:请参阅我的第二次编辑(使用For Loop
的示例代码)@欣快我认为你的否决票是不合理的,因为原始问题和概念被编辑了3次,我没有跟踪所有的更改。第二个显示了一些错误,如“无法隐式转换类型”字符串“为”bool”`
ListOne
A
B
C
ListTwo
A
B
C
D
A-B
A-C
A-D
B-C
B-D
C-D
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));
It produces like
A-B
A-C
A-D
B-A
B-C
B-D
C-A
C-B
C-D
A-B
A-C
A-D
B-C
B-D
C-D
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]);
}
}
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);
}
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];
IEnumerable<string> result =
(
from a in ListOne
from b in ListTwo
where a != b
select a.CompareTo(b) < 0 ? a + "-" + b : b + "-" + a
).Distinct();
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)));
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;
}
}
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);
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);
}
}
}