首页 > 解决方案 > Dynamic Linq in Asp.net Core?

问题描述

I have a collection of Json data that I convert into a List<Dictionary<string, object>> I have to then filter that list and select some column out but the conditions of what to use for the filter/select will be different depending on different scenarios.

How can I dynamically build a linq statement?

items.Where(x => x.ContainsKey("A") && x.ContainsKey("B") && x.ContainsKey("C"))
        .Select(x => new { a = (string)x["A"], b = (string)x["B"], c = Convert.ToInt32(x["C"]) }).ToList();

Sometimes it maybe an "or" statement instead of "and". I do anticipate the where clause conditions will match the select conditions.

标签: c#linq

解决方案


推荐阅读