Csharp/C#教程:有没有机会使用Linq(C#)获得独特的记录?分享


有没有机会使用Linq(C#)获得独特的记录?

我有一个list<list>

list[x][0]是我想要选择唯一记录的记录,因此这样的记录不会出现在任何其他list[x][0 ]中,当我选择它时,我想要整行list[x]被选中。 我还没有在Linq找到适当的这个例子,请帮忙:(

编辑

当Jon Skeet要求我澄清时,我不能否认;-)

 list<list> 

包含字符串表的列表。 每个字符串“table”包含几个键list[x][several_items] several list[x][several_items] ,我想从list->中获取唯一记录,这意味着该“表”中的第一项。

从而:

 item[0] = "2","3","1","3" item[1] = "2","3","4","2" item[3] = "10","2" item[4]= "1","2" 

– > unique意味着我可以将行item[3] and item[4]派生为唯一。 因为第一次出现数字/字符串很重要。

如果列表中有多个记录/行( item[x] of which first item (item[x][0])存在多次,则它不是唯一的。

每个列表的第一个元素对于确定唯一性很重要。 也许如果有人可以帮助找到找到非唯一的方法会更容易 – >所以从上面的例子我只得到项目[0]和项目[1]

编辑:我已经更新了底部的UniqueBy实现,效率更高,并且只迭代源一次。

如果我理解正确(问题非常清楚 – 如果你能提供一个例子,那将会非常有帮助)这就是你想要的:

 public static IEnumerable OnlyUnique(this IEnumerable source) { // No error checking :) HashSet toReturn = new HashSet(); HashSet seen = new HashSet(); foreach (T element in source) { if (seen.Add(element)) { toReturn.Add(element); } else { toReturn.Remove(element); } } // yield to get deferred execution foreach (T element in toReturn) { yield return element; } } 

编辑:好的,如果您只关心列表中第一个唯一性元素,我们需要稍微改变一下:

 public static IEnumerable UniqueBy (this IEnumerable source, Func keySelector) { var results = new LinkedList(); // If we've seen a key 0 times, it won't be in here. // If we've seen it once, it will be in as a node. // If we've seen it more than once, it will be in as null. var nodeMap = new Dictionary>(); foreach (TElement element in source) { TKey key = keySelector(element); LinkedListNode currentNode; if (nodeMap.TryGetValue(key, out currentNode)) { // Seen it before. Remove if non-null if (currentNode != null) { results.Remove(currentNode); nodeMap[key] = null; } // Otherwise no action needed } else { LinkedListNode node = results.AddLast(element); nodeMap[key] = node; } } foreach (TElement element in results) { yield return element; } } 

你打电话给:

 list.UniqueBy(row => row[0]) 

也许是这样的事情?

鉴于你的澄清,我现在相当确定这会对你有用:)

 var mylist = new List>() { new List() { "a", "b", "c" }, new List() { "a", "d", "f" }, new List() { "d", "asd" }, new List() { "e", "asdf", "fgg" } }; var unique = mylist.Where(t => mylist.Count(s => s[0] == t[0]) == 1); 

unique现在包含上面的“d”和“e”条目。

这是您需要的代码。 它非常适合我选择只有不同的值。

 //distinct select in LINQ to SQL with Northwind var myquery = from user in northwindDC.Employees where user.FirstName != null || user.FirstName != "" orderby user.FirstName group user by user.FirstName into FN select FN.First(); 

这是给你的一些Linq。

 List> Records = GetRecords(); // List UniqueRecords = Records .GroupBy(r => r[0]) .Where(g => !g.Skip(1).Any()) .Select(g => g.Single()) .ToList(); 

我会继续把这个添加到战斗中。

 using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List xx = new List() { "xx", "yy", "zz" }; List yy = new List() { "11", "22", "33" }; List zz = new List() { "aa", "bb", "cc" }; List> x = new List>() { xx, yy, zz, xx, yy, zz, xx, yy }; foreach(List list in x.Distinct()) { foreach(string s in list) { Console.WriteLine(s); } } } } } 

您可以维护一个列表和一个索引/ 字典 :

 List> values; Dictionary> index; 

向值添加项时,还将List添加到索引,并将字符串作为索引。

 values[x].Add(newString); index[newString] = values[x]; 

然后你可以通过以下方式获得正确的列表

 List list = index[searchFor] 

在构建索引时,您会丢失一些(最小)性能和内存,但在检索数据时会获得很多收益。

如果字符串不是唯一的,您还可以在字典 /索引中存储List>,以允许每个索引键有多个结果。

对不起没有Linq,这看起来不那么酷,但你有快速查找,而且恕我直言,查找代码更清晰。

上述就是C#学习教程:有没有机会使用Linq(C#)获得独特的记录?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1000596.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐