查询动态对象的列表 [英] query a list of dynamic objects

查看:153
本文介绍了查询动态对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用大规模得到配置表在数据库中。我想缓存配置,因为应用程序获取价值观从它所有的时间。

I am using massive to get the config table in the database. I would like to cache the config since the app gets values from it all the time.

在缓存有一个简单的方法来找到对象,其中name ='东西'

once cached is there an easy way to find the object where name = 'something'

这里是整个表缓存。

    protected override dynamic Get()
    { 
        var ret = HttpRuntime.Cache["Config"]; 
        if (ret == null)
        { 
            ret = _table.All(); 
            HttpRuntime.Cache.Add("Config", ret, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration,CacheItemPriority.Low, null );
        }
        return ret; 
    } 

下面是我想拉一个记录从方法

here is where I would like to pull one record from that method

    protected override dynamic Get(string name)
    {
        return this.Get().Where(x => x.Name == name ).SingleOrDefault(); 
    }

我知道LINQ或lambda语句在动态对象允许的。但什么是下一个最好的的方式来拉,一个物体从该列表中的?

I know linq or lambda statements are not allowed in dynamic objects. but what is the next best way to pull that one object out of that list?

推荐答案

您不能直接写LAMDA EX pression作为其中的说法,但你可以把它分配给函数求变量。 此外,我认为扩展方法不会对动态对象的工作,所以你必须直接调用扩展方法。

You could not write the lamda expression directly as the Where argument, but you could assign it to a Func variable. Also I believe extension methods would not work on dynamic objects, so you have to call the extension method directly.

我想你可以用下面的code,

I think you could use the below code,

        Func<dynamic, bool> check = x => x.Name == name;
        System.Linq.Enumerable.Where<dynamic>(this.Get(), check);

这篇关于查询动态对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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