从LinqToXYZ切换到LinqToObjects [英] Switching from LinqToXYZ to LinqToObjects

查看:173
本文介绍了从LinqToXYZ切换到LinqToObjects的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题,这让我想...

In answering this question, it got me thinking...

我经常使用这种模式:

collectionofsomestuff //here it's LinqToEntities
    .Select(something=>new{something.Name,something.SomeGuid})
    .ToArray() //From here on it's LinqToObjects
    .Select(s=>new SelectListItem()
        {
            Text = s.Name, 
            Value = s.SomeGuid.ToString(), 
            Selected = false
        })

也许我把它分解了几行,但本质上,在的ToArray 点,我列举有效我的查询和存储产生的序列,这样我可以进一步与一个完整的CLR交出所有的善良处理。

Perhaps I'd split it over a couple of lines, but essentially, at the ToArray point, I'm effectively enumerating my query and storing the resulting sequence so that I can further process it with all the goodness of a full CLR to hand.

由于我有没有兴趣在任何一种中间列表操纵,我使用的ToArray 了ToList 因为有开销少。

As I have no interest in any kind of manipulation of the intermediate list, I use ToArray over ToList as there's less overhead.

我做这一切的时候,但我不知道是否有对这类问题的一个更好的模式?

I do this all the time, but I wonder if there is a better pattern for this kind of problem?

推荐答案

有一个更好的选择: AsEnumerable

使用方法是相似的:

collectionofsomestuff //here it's LinqToEntities
    .Select(something=>new{something.Name,something.SomeGuid})
    .AsEnumerable() //From here on it's LinqToObjects
    .Select(s=>new SelectListItem()
        {
            Text = s.Name, 
            Value = s.SomeGuid.ToString(), 
            Selected = false
        })

然而,这并不强制完全复制像了ToList()或 ToArray的(),并保留从供应商的任何延迟执行。

This, however, doesn't force a full copy to be made like ToList() or ToArray(), and preserves any deferred execution from your provider.

这篇关于从LinqToXYZ切换到LinqToObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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