我有两个List<String>
,其中包含
ListOne
A
B
C
ListTwo
A
B
C
D
现在我需要将移动组合转换为列表字符串
因此输出列表将包含
A-B
A-C
A-D
B-C
B-D
C-D
现在我正在使用Nested for loop
。
有没有办法使用LINQ
或LAMBDA EXPRESSION
执行此操作
请帮我这样做。
提前致谢
示例代码
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
使用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]);
}
}
它的工作正常....但我只需要一个简单的方法(使用LINQ
)
答案 0 :(得分:8)
在您编辑之后,这应该可以解决问题:
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);
}
保留订单的第二个版本(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];
答案 1 :(得分:2)
所以你想要除
之外的所有比赛并且没有重复。
这是:
IEnumerable<string> result =
(
from a in ListOne
from b in ListTwo
where a != b
select a.CompareTo(b) < 0 ? a + "-" + b : b + "-" + a
).Distinct();
说明要求是战斗的90%。
如果您需要配对中的第一项来自第一个列表,则可以执行此操作:
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()
)
答案 2 :(得分:2)
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)));
答案 3 :(得分:0)
如何将验证逻辑添加到列表中
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;
}
}
答案 4 :(得分:0)
我不认为LINQ对此有好处,我得到了这个,但它看起来并不好看:
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,当我清理你的嵌套foreach代码时,我比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);
}
}
}