如何使用 LINQ 列出另一个列表中的所有重复字符串
How to make a list of all duplicate strings in another list with LINQ
我找到了一些类似的主题,但没有一个适合我的需要。我有一个字符串列表,如下所示:
List<List<string>> myList = {Foo, Foo, Bar, UniqueValue, Bar}
我想使用 Linq 制作一个 List<List<string>>
给我 3 个列表,每个列表都包含我上面列表中的唯一值。像这样:
List<List<string>> Entries =
{
List<List<string>> EntriesNamedFoo = {Foo, Foo, Foo}
List<List<string>> EntriesNamedBar = {Bar, Bar}
List<List<string>> EntriesNamedUniqueValue = {UniqueValue}
}
//Any given list would look like this:
UniqueValue = {AccountNumber, InvoiceDate, ProfessionalJargon, SomethingElseINeed}
//So I want to sort my lists by into groups that have the same value at list[0].
在我的代码中,我有一个带帐户的分隔文件。对于每个客户,我想生成一份报告,所以我想按客户 ID 对帐户进行分组。我单独制作了 PDF,我想将共享相同客户 ID 的文件合并到一个文件中。
我可以在没有 LINQ 的情况下完成此操作,但我对如何使用 LINQ 完成此操作非常感兴趣。
最后,我希望能够快速确定唯一项的数量。
在 Linq 中,您可以像这样计算唯一值的数量:
items.Select(i => i.Value).Distinct().Count();
但我的问题是我的 "items" 实际上是一个二维数组。我有 50 行和 10 列。我想查看每一行(在其第一个元素处)并计算该位置的唯一值的数量。
你们有任何提示都很棒,谢谢!
您需要像这样使用GroupBy method:
var grouped = original.GroupBy(s => s)
.Select(grp => grp.ToList())
.ToList();
请注意,所有 ToList
调用只是为了确保您在任何地方都有 List<string>
,否则您将有 IEnumerable<string>
,这取决于您想要做什么可以就好了。
var distinctClientd = myListOfLists.Where(l => l.Any())
.GroupBy(l => l.First())
.Select( g => g.ToList());
是吗?老实说,很难准确理解你在问什么。
如果我理解你的问题:
List<string> myList = new List<string> { "Foo", "Foo", "Bar", "UniqueValue", "Bar" };
var listGrouped = myList.GroupBy( x => x, //the element you want to group by
(key, //the element you grouped by
element //the new list of strings grouped
)=> new
{
Key = key,
Count = element.Count()
});
foreach (var item in listGrouped)
{
Console.WriteLine("- - - - - - - -");
Console.WriteLine(item.Key);
Console.WriteLine(item.Count);
}
Console.ReadKey();
它给你这个结果:
- - - - - - - -
Foo
2
- - - - - - - -
Bar
2
- - - - - - - -
UniqueValue
1
我找到了一些类似的主题,但没有一个适合我的需要。我有一个字符串列表,如下所示:
List<List<string>> myList = {Foo, Foo, Bar, UniqueValue, Bar}
我想使用 Linq 制作一个 List<List<string>>
给我 3 个列表,每个列表都包含我上面列表中的唯一值。像这样:
List<List<string>> Entries =
{
List<List<string>> EntriesNamedFoo = {Foo, Foo, Foo}
List<List<string>> EntriesNamedBar = {Bar, Bar}
List<List<string>> EntriesNamedUniqueValue = {UniqueValue}
}
//Any given list would look like this:
UniqueValue = {AccountNumber, InvoiceDate, ProfessionalJargon, SomethingElseINeed}
//So I want to sort my lists by into groups that have the same value at list[0].
在我的代码中,我有一个带帐户的分隔文件。对于每个客户,我想生成一份报告,所以我想按客户 ID 对帐户进行分组。我单独制作了 PDF,我想将共享相同客户 ID 的文件合并到一个文件中。
我可以在没有 LINQ 的情况下完成此操作,但我对如何使用 LINQ 完成此操作非常感兴趣。
最后,我希望能够快速确定唯一项的数量。
在 Linq 中,您可以像这样计算唯一值的数量:
items.Select(i => i.Value).Distinct().Count();
但我的问题是我的 "items" 实际上是一个二维数组。我有 50 行和 10 列。我想查看每一行(在其第一个元素处)并计算该位置的唯一值的数量。
你们有任何提示都很棒,谢谢!
您需要像这样使用GroupBy method:
var grouped = original.GroupBy(s => s)
.Select(grp => grp.ToList())
.ToList();
请注意,所有 ToList
调用只是为了确保您在任何地方都有 List<string>
,否则您将有 IEnumerable<string>
,这取决于您想要做什么可以就好了。
var distinctClientd = myListOfLists.Where(l => l.Any())
.GroupBy(l => l.First())
.Select( g => g.ToList());
是吗?老实说,很难准确理解你在问什么。
如果我理解你的问题:
List<string> myList = new List<string> { "Foo", "Foo", "Bar", "UniqueValue", "Bar" };
var listGrouped = myList.GroupBy( x => x, //the element you want to group by
(key, //the element you grouped by
element //the new list of strings grouped
)=> new
{
Key = key,
Count = element.Count()
});
foreach (var item in listGrouped)
{
Console.WriteLine("- - - - - - - -");
Console.WriteLine(item.Key);
Console.WriteLine(item.Count);
}
Console.ReadKey();
它给你这个结果:
- - - - - - - - Foo 2 - - - - - - - - Bar 2 - - - - - - - - UniqueValue 1