问题
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