C# 寻找可能的组合linq
我需要在C# 寻找可能的组合linq,c#,linq,permutation,C#,Linq,Permutation,我需要在{“a”、“b”、“c”}之间生成所有可能的组合 例如,一个输入集说像{a”,“b”,“c”},预期的输出是{a”,“b”,“c”“ab”,“ac”,“bc”,“abc”},听起来你要找的基本上是一种形式的。下面是一个简单的实现(摘自): 或者更简单地说: public IEnumerable<string> GetPermutations(IList<string> strings) { return from s in strings.GetPower
{“a”、“b”、“c”}
之间生成所有可能的组合
例如,一个输入集说像
{a”,“b”,“c”}
,预期的输出是{a”,“b”,“c”“ab”,“ac”,“bc”,“abc”}
,听起来你要找的基本上是一种形式的。下面是一个简单的实现(摘自):
或者更简单地说:
public IEnumerable<string> GetPermutations(IList<string> strings)
{
return from s in strings.GetPowerSet().Skip(1)
select string.Concat(s);
}
public IEnumerable GetPermutations(IList字符串)
{
从字符串中的返回。GetPowerSet()。跳过(1)
选择string.Concat;
}
到目前为止,您尝试了什么这看起来不像是Linq非常适合解决的问题。简单的旧迭代是一种方法。我试过使用Eric Lippert的[Cartesian product](),但没有帮助。我编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。
public IEnumerable<string> GetPermutations(IList<string> strings)
{
return from s in strings.GetPowerSet()
select string.Concat(s);
}
public IEnumerable<string> GetPermutations(IList<string> strings)
{
return from s in strings.GetPowerSet()
let str = string.Concat(s)
where str.Length > 0 // exclude null set result
select str;
}
public IEnumerable<string> GetPermutations(IList<string> strings)
{
return from s in strings.GetPowerSet().Skip(1)
select string.Concat(s);
}