query a list of dynamic objects

独自空忆成欢 提交于 2019-12-24 01:04:14

问题


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.

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

here is where the whole table is cached.

    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(); 
    }

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?


回答1:


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.

I think you could use the below code,

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


来源:https://stackoverflow.com/questions/8828105/query-a-list-of-dynamic-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!