有没有办法说LINQ列表是排序的......?

时间:2017-05-17 08:00:16

标签: c# linq

我在数据库中有一些大数据,我需要按索引对项目进行分组, 例如

data[0] = > property_1 = 'zxc', property_2='xxx'

在数据库中我的记录如下:

data[0]/property_1/zxc
data[1]/property_1/zzz
data[0]/property_2/xxx
data[3]/property_1/ooo

所以我可以在数据库中对它们进行排序(它很快,select语句只能获得1秒) 但如果我想在linq中对它们进行grup,那就非常慢了

重要:由于某些原因,我无法在数据库中对它们进行格式化,我必须在应用程序中进行此操作

1 个答案:

答案 0 :(得分:3)

没有。 request = service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') with open('data.xlsx', 'wb') as f: f.write(request.execute()) 无法利用这些知识。

但是,如您所知输入已排序,您可以针对您的假设情况编写一个不太通用的Enumerable.GroupBy类似方法。

(但是在数据库上进行分组会更好。)

按照关键顺序假设这样的自定义组看起来像:

GroupBy

其中IEnumerable<IGrouping<TKey, TElement>> InputOrderedGroupBy<T, TKey>( this IEnumerable<T> input, Func<T, TKey> keyExtractor) { TKey currKey = default(TKey); Grouped<T, TKey> res = null; foreach (var t in input) { var thisKey = keyExtractor(t); if (res == null || thisKey != currKey) { if (res != null) { yield return res; } res = new Grouped<T, TKey>(); res.Key = currKey = thisKey; } res.Collection.Add(t); } if (res != null) { yield return res; } } Grouped<T, TKey>的一些实现。

相关问题