如何在linq或where子句中放置动态列名 [英] How to put dynamic column name in linq or where clause
本文介绍了如何在linq或where子句中放置动态列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要带有动态列的lemda表达式,如下例所示
I need a lemda expression with dynamic column like below example
var item=listItem.Where(DynamicColumn.Contains(textValue));
或
or
var item=listItem.Where(a=>a.DynamicColumn.Contains(textValue));
我尝试了什么:
我试过这样做的方式与我在问题描述,但它不起作用。
What I have tried:
I have tried to do it by the same way i have discussed in the question description but it's not working.
推荐答案
解决方案是动态构建所需的表达式树,通常使用这些LINQ方法为您引导 。
这是一个中等高级的话题,但你不必自己动手:已经有几种解决方案你可以使用:
动态构建Lambda表达式 [ ^ ]
Dynamic Expresso [ ^ ]
运行时的LINQ和动态谓词构建 [ ^ ]
在Linq中动态构建子句 [ ^ ]
<啊ref =https://github.com/kahanu/System.Linq.Dynamic> GitHub - Kahanu / System.Linq.Dynamic:这是.Net 4.0动态语言功能的Microsoft程序集。 [< a href =https://github.com/kahanu/System.Linq.Dynamictarget =_ blanktitle =New Window> ^ ]
The solution is to dynamically build the required expression tree which normally gets done for you "under the hood" by using those LINQ methods.
That's a moderately advanced topic but you don't have to do it yourself: There already are several solutions for this which you could use:
Build Lambda Expressions Dynamically[^]
Dynamic Expresso[^]
LINQ and Dynamic Predicate Construction at Runtime[^]
Build Where Clause Dynamically in Linq[^]
GitHub - kahanu/System.Linq.Dynamic: This is the Microsoft assembly for the .Net 4.0 Dynamic language functionality.[^]
这篇关于如何在linq或where子句中放置动态列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文