嗨,如何从包含重复项的int字符串对列表中获取唯一字段 [英] Hi, , how to get unique fields from a list of int string pair which contains duplicates
问题描述
列表与LT;整型,字符串> list = new List< int,string>();
//例如,如果我的列表中的值是
list [0] = {100,abc}
list [1] = {101,zzz}
list [2] = {103,aaa}
list [3] = {100,yyy}
//我希望我的列表只返回唯一值
//我想让我的列表像这样返回。
list = {101,zzz; 103,aaa};
< b>我尝试了什么:
使用了带有Distinct()的linq,但它在第一次出现时给了我非唯一字段。
List类不支持多个维度。在此处阅读更多相关信息:< list> (C#编程指南)| Microsoft Docs [ ^ ]
如果你想使用List类,那么你可以使用Array,Class, [ ^ ]元组或 KeyValuePair [< a href =https://msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ] to to存储你的价值观。对于此解决方案,我将使用List< keyvaluepair>因为你有非唯一的密钥。 词典 [ ^ ]类需要唯一键,因此不适合。
var list = new 列表< KeyValuePair< ; int,string>>
{
new KeyValuePair< int,string> ( 100 , abc),
new KeyValuePair< int,string> ( 101 , zzz),
new KeyValuePair< int,string> ( 103 , aaa),
new KeyValuePair< int,string>( 100 , yyy)
};
var results = list.GroupBy(x = > x.Key )
。其中(x = > x.Count()== 1 )
。选择(x = > x.First());
foreach ( var item in results)
{
Console.WriteLine(
Key:{item.Key} Value:{item.Value});
}
Linq表达式将按Key字段分组,当每个分组只有1个唯一结果时,选择第一个条目。
尝试
列表< KeyValuePair< int,string>> lst = new List< KeyValuePair< int,string>>();
lst.Add(new KeyValuePair< int,string>(100,abc));
lst.Add(new KeyValuePair< int,string>(101,zzz));
lst.Add(new KeyValuePair< int,string>(103,aaa));
lst.Add(new KeyValuePair< int,string>(100,yyy));
int [] uniqueItems = lst.GroupBy(k => k.Key)。其中(k => k.Count()== 1)。选择(k = > k.Key)。ToArray();
lst = lst.Where(k => uniqueItems.Contains(k.Key))。ToList(); //列表= {101,ZZZ; 103,AAA};
List<int,string> list = new List<int,string>();
//Example say, if the values in my list is
list[0] ={100, "abc"}
list[1] ={101, "zzz"}
list[2] ={103, "aaa"}
list[3] ={100, "yyy"}
//I want my list to return only unique values
//I want my list to return like this.
list={101,zzz;103,aaa};
What I have tried:
have used linq with Distinct() but it gives me non unique field with first occurrence.
The List class does not support multiple dimensions. Read more about it here: <list> (C# Programming Guide) | Microsoft Docs[^]
If you want to use a List class, then you can use a Array, Class, [^]Tuple or KeyValuePair[^] to store your values. For this solution I will use the List<keyvaluepair> as you have non-unique keys. The Dictionary[^] class requires unique keys so is not suitable.
var list = new List<KeyValuePair<int, string>> { new KeyValuePair<int, string> (100, "abc"), new KeyValuePair<int, string> (101, "zzz"), new KeyValuePair<int, string> (103, "aaa"), new KeyValuePair<int, string>(100, "yyy") }; var results = list.GroupBy(x => x.Key) .Where(x => x.Count() == 1) .Select(x => x.First()); foreach (var item in results) { Console.WriteLine(
"Key: {item.Key} Value: {item.Value}"); }
The Linq expression will group by the Key field, when where there is only 1 unique result per grouping, select the first entry.
try
List<KeyValuePair<int, string>> lst = new List<KeyValuePair<int, string>>(); lst.Add(new KeyValuePair<int, string>(100, "abc")); lst.Add(new KeyValuePair<int, string>(101, "zzz")); lst.Add(new KeyValuePair<int, string>(103, "aaa")); lst.Add(new KeyValuePair<int, string>(100, "yyy")); int[] uniqueItems = lst.GroupBy(k => k.Key).Where(k => k.Count() == 1).Select(k => k.Key).ToArray(); lst = lst.Where(k => uniqueItems.Contains(k.Key)).ToList(); //list={101,zzz;103,aaa};
这篇关于嗨,如何从包含重复项的int字符串对列表中获取唯一字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!