什么是你最喜欢的LINQ的方法或“绝招” [英] What's your favourite linq method or 'trick'

查看:105
本文介绍了什么是你最喜欢的LINQ的方法或“绝招”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常主观的问题(和一个可能会看到的关闭票乱舞),因此它指向的CW和添加免责声明。

I know it's a highly subjective issue (and one that may well see a flurry of 'close it' votes), hence pointing it at the CW and adding the disclaimer.

不管怎样,我一直在使用亚音速工作救援人员到场一个项目,asp.net的MVC和突然(再次)来袭的elegence和LINQ执行耐用性是位于两个亚音速和普通的LINQ to顶对象库。这惹起了我看起来有点接近了我的code,使我认识到,一些我使用LINQ的任务是无限复杂的(或几乎不可能 - 主观)当然,如果试图以传统方式(这当然很可能是对我的限制)。

Anyway, I've been working thro a project in asp.net mvc using subsonic and was suddenly (again) struck with the elegence and 'ruggedness' of the linq implementation that lies on top of both subsonic and the general linq to objects library. This occasioned me look a little closer into my code and made me realise that some of the tasks that I was using linq for would be endlessly complex (or 'near impossible' - subjective of course) if attempted in a traditional manner (this of course could well be a limitation in me).

无论如何,这样的结果,我认为这将是有趣的,看看有什么问题的民间曾遇到了elegently使用LINQ解决。

Anyway, as a result of this, I thought it would be interesting to see what problems folk had encountered that were elegently solved by using linq.

要揭开序幕,我最近已经涉及到了一个共同的'unitid'属性上有一个实体中的一个场景。我希望能够将一组这些以操控和审查这一集时分裂出thier适当的unitid团体。左思右想,我来到了这个简单的小LINQ单行:

To kick off, one scenario that i recently had involved an entity that had a common 'unitid' property on it. i wanted to be able to group a set of these in order to manipulate and examine this set when split out to thier appropriate 'unitid' groups. After much thought, i arrived at this simple little linq one liner:

// the base collection of the objects - not grouped in any way
IQueryable<ShareholderRSP> rsps = validshareholderPounds
    .Where(x => x.Balance > 0)
    .OrderBy(x => x.RequestYear);

// the one-liner in question to group the objects by unitid
IList<IGrouping<string, ShareholderRSP>> groupedRSP =
     rsps.ToList().GroupBy(t => t.UnitID.ToString()).ToList();

我当时能够为使用:

which i was then able to use as:

foreach (var rspGrpItem in groupedRSP)
{
    foreach (var shareholderRSP in rspGrpItem)
    {
       // note, i could have also iterated this list too 
       // which is a base object contained within the collection
       // underneath the group collection item
    }
    // create a top level summary grouped by unitid
    // usedPounds is a variable created earlier on in the piece
    bookingConfirm.RSPUnitSelect.Add(
        new BookingConfirmRSPSelect
        {
            TotalBeforeUse = rspGrpItem.Sum(rsp => rsp.Balance),
            TotalAfterUse = rspGrpItem.Sum(rsp => rsp.Balance) - usedPounds,
            UnitNo = rspGrpItem.Key
        }
    );
}

如果我在一个传统的方式尝试这个,我的逻辑会(在我看来)已经比较麻烦,不太能适应底层对象模型的任何更改。

Had i attempted this in a traditional fashion, my logic would (in my opinion) have been more cumbersome and less able to adapt to any changes in the underlying object model.

我们希望,如果没有相当的讨论电火花,这也突显我的'欢乐'在使用LINQ。

Hopefully, if not quite a discussion sparker, this has highlighted my 'joy' at using linq.

所有最好的 - 希望遗漏上面所讨论的对象模型不会从这个例子的目的减损,因为有希望的意图显露所有。

All the best - hopefully the omission of the object model in question above won't detract from the purpose of the example, as hopefully the intention reveals all.

展望FWD一些美好的例子。

Looking fwd to some fine examples.

吉姆

推荐答案

简单的一招:一个快速的方法来产生随机数序列:

Simple trick : a quick way to generate a sequence of random numbers :

var randomNumbers = Enumerable.Repeat(new Random(), 10).Select(r => r.Next());

这篇关于什么是你最喜欢的LINQ的方法或“绝招”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆