Dynamic Linq Select -How to extract the results

↘锁芯ラ 提交于 2019-12-12 04:59:58

问题


I have a dynamic Linq Select statement of the form

var projection = result.AsQueryable().Select(string.Format("new({0},{1})",
         model.XtabRow, model.XtabColumn));

This works fine and produces an IQueryable of Anonymous types.

However I an unable to convert it into IEnumerable to use linq on it as AsEnumerable method seems to be missing. I had to use reflection to extract field values in the end

There must be a better way - Any help would be great

Thanks


回答1:


You can try something like this

var projection = result.AsQueryable().Select(string.Format("new({0},{1})",
     model.XtabRow, model.XtabColumn));

var enumerableProjection = (from dynamic p in projection select p).AsEnumerable();

OR

var projection = result.AsQueryable().Select(string.Format("new({0},{1})",
     model.XtabRow, model.XtabColumn));

var enumerableProjection = projection.Cast<dynamic>().AsEnumerable();


来源:https://stackoverflow.com/questions/22534191/dynamic-linq-select-how-to-extract-the-results

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