如何在LINQ中动态写在哪里? [英] How to write dynamic where in LINQ?
本文介绍了如何在LINQ中动态写在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的LinQ查询
I have a LinQ query as below
var filtered=from c in country
where c.Id>=5
select c;
现在我需要让运算符``> =''是动态的,即运算符可能是``> =''或``==''或``< =''等,具体取决于条件和查询应基于该运算符执行.
Now i need to have the operator ''>='' to be dynamic i.e., the operator may be ''>='' or ''=='' or ''<='' etc depending on a certain criteria and the query should execute based on that operator. How to achieve this in LinQ?
推荐答案
您可以添加一个返回bool的检查功能.
像这样的东西:
Hi,
You could add a check-function which returns a bool.
Something like this:
private bool TestValue(int id, int contiditon)
{
if (condition == 1)
{
return id <= 5;
}
else
{
return id >= 5;
}
}
/* your linq query would look like this */
var filtered = from c in country
where TestValue(c.Id, c.Condition)
select c;
这是完全未经测试的,因此可能包含错误;)
希望这会有所帮助.
致以诚挚的问候,祝您编程愉快,
停止
This is completely untested so it could contain errors ;)
Hope this helps.
Best regards and happy coding,
Stops
如何在水晶报表中使用关闭按钮
How to use the close button in crystal report
这篇关于如何在LINQ中动态写在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文