从大型集合中获取前10条记录并使用自定义OrderBy的好方法是什么?如果我使用LINQ to Objects OrderBy方法,它会很慢并占用大量内存,因为它会使用新订单创建一个完整的新集合.我想要一个带有下面签名的新方法,它不会重新整理整个集合并且非常快:
public static IEnumerable<TSource> OrderByTop<TSource, TKey>(
IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
int topCount)
Run Code Online (Sandbox Code Playgroud)
我试着写它但它变得非常复杂,我想可能有更简单的方法使用Aggregate或其他东西.任何帮助,将不胜感激.
回答
谢谢您的帮助.我最终得到了以下代码:
public static List<TSource> OrderByTop<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
int topCount)
{
var itemComparer = keySelector.ToIComparer(comparer);
return source.Aggregate(
new List<TSource>(topCount),
(List<TSource> list, TSource item) =>
list.SortedInsert(item, itemComparer, topCount));
}
Run Code Online (Sandbox Code Playgroud)
List Extension方法SortedInsert如下:
public static List<T> SortedInsert<T>(
this List<T> list,
T item,
IComparer<T> comparer,
int maxLength)
{
if (list.Count == maxLength)
if (comparer.Compare(item, list[maxLength - 1]) >= …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
Event thisEvent = (from i in list
where (i.eventID == eventID)
select i).FirstOrDefault();
if (thisEvent != null)
{
thisEvent.eventResolved = resolved;
thisEvent.eventSequence.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
"list"是IEnumerable的集合,即
IEnumerable<Event> list;
Run Code Online (Sandbox Code Playgroud)
我想知道的是:在使用FirstOrDefault创建thisEvent之后,thisEvent是否仍然连接到列表?换句话说,当我更改两个属性,eventResolved和eventSequence时,实际更改了"list",或者thisEvent只是"list"中项目的一些完全断开的副本?
当我需要知道为什么我的LINQ无法按预期工作时,我经常处于一个位置......我使用对象集合和扩展.
我不想花费超过几分钟的时间.LINQ应该让开发人员的生活更轻松而不是更难.
我希望VS 2010会修复它但我现在使用RC并且它仍然不允许我键入LINQ并检查发生了什么...说以前"表达式不能包含lambda表达式"...
是否有一些Visual Studio的附加组件,因此我可以快速有效地运行即席查询并找出发生了什么以及我错在哪里?
为什么在RepeaterItemCollection上没有LINQ扩展方法,尽管它实现了IEnumerable?
我正在将Linq用于同一类中其他地方的对象.但是,当我尝试使用RepeaterItemCollection时,它们不可用.我的印象是LINQ扩展方法可用于实现IEnumerable的类.
我错过了什么?
我在C#中使用Lookup类作为我的主要数据容器,供用户从两个Checked List框中选择值.
Lookup类比使用类Dictionary更容易使用,但是我找不到用于删除和向查找类添加值的方法.
我想过使用where和union,但我似乎无法正确使用它.
提前致谢.
var nums = new[]{ 1, 2, 3, 4, 5, 6, 7};
var pairs = /* some linq magic here*/ ;
Run Code Online (Sandbox Code Playgroud)
=> pairs = {{1,2},{3,4},{5,6},{7,0}}
元素pairs应该是两元素列表,或者是一些具有两个字段的匿名类的实例,例如new {First = 1, Second = 2}.
我需要通过降序发布日期来订购存储在数据库中的文章,然后在文章后面的前20条记录Id == 100.
这就是我想对Linq做的事情:
IQueryable<Article> articles =
db.Articles
.OrderByDescending(a => a.PublicationDate)
.SkipWhile(a => a.Id != 100)
.Take(20);
Run Code Online (Sandbox Code Playgroud)
但是,这会生成NotSupportedException,因为SkipWhileLinq to Sql不支持(请参阅此处).
一种可能的解决方案是执行查询,然后SkipWhile使用Linq应用于Object:
IEnumerable<ArticleDescriptor> articles =
db.Articles
.OrderByDescending(a => a.PublicationDate)
.ToList()
.SkipWhile(a => a.Article.Id != 100)
.Take(20);
Run Code Online (Sandbox Code Playgroud)
但这意味着我需要首先将整个有序列表加载到内存中,然后在一个文件后加载20篇文章Id == 100.
有没有办法避免这种巨大的内存消耗?
更一般地说,在SQL中实现这一目标的最佳方法是什么?
MyClass包括ID ParentID与List<MyClass>作为Children
我的名单MyClass像这样
ID ParentID
1 0
2 7
3 1
4 5
5 1
6 2
7 1
8 6
9 0
10 9
Run Code Online (Sandbox Code Playgroud)
输出(分层列表)为 List<MyClass>
1 __ 3
|__ 5__ 4
|__ 7__ 2__ 6__ 8
|__ 11
9 __10
Run Code Online (Sandbox Code Playgroud)
在linq中实现这一目标的最简单方法是什么?
PS:ParentID没有排序
编辑:
我的尝试:
class MyClass
{
public int ID;
public int ParentID;
public List<MyClass> Children = new List<MyClass>();
public MyClass(int id, int parent_id)
{
ID = id; …Run Code Online (Sandbox Code Playgroud) 鉴于以下代码设置:
public class Foo {
List<string> MyStrings { get; set; }
}
List<Foo> foos = GetListOfFoosFromSomewhere();
Run Code Online (Sandbox Code Playgroud)
如何使用LINQ获取所有Foo实例中MyStrings中所有不同字符串的列表?我觉得这应该很容易,但不能完全理解.
string[] distinctMyStrings = ?
Run Code Online (Sandbox Code Playgroud) 好的,所以我在C#中有两个列表
List<Attribute> attributes = new List<Attribute>();
List<string> songs = new List<string>();
Run Code Online (Sandbox Code Playgroud)
一个是字符串,一个是我创建的属性对象.非常简单
class Attribute
{
public string size { get; set; }
public string link { get; set; }
public string name { get; set; }
public Attribute(){}
public Attribute(string s, string l, string n)
{
size = s;
link = l;
name = n;
}
}
Run Code Online (Sandbox Code Playgroud)
我现在必须比较一下,看看哪些歌曲不在属性名称中,例如
songs.Add("something");
songs.Add("another");
songs.Add("yet another");
Attribute a = new Attribute("500", "http://google.com", "something" );
attributes.Add(a);
Run Code Online (Sandbox Code Playgroud)
我想要一种方法来返回"另一个"和"另一个",因为它们不在属性列表名称中
所以对于伪代码
difference = songs - attributes.names
Run Code Online (Sandbox Code Playgroud) linq-to-objects ×10
linq ×7
c# ×6
list ×2
aggregate ×1
c#-3.0 ×1
collections ×1
debugging ×1
distinct ×1
ienumerable ×1
linq-to-sql ×1
lookup ×1
performance ×1
repeater ×1
slice ×1
sql ×1
sql-order-by ×1