LINQ:如何向 LINQ 查询添加多个变量?
我有一个名为 List 的控制器操作,它采用以下参数
int Filer、int Field、int Operator、string QueryValue
public enum Filter
{
ActiveEmployee,
OnHoldEmployee,
InactiveEmployee
}
public enum Field
{
Name,
ABRAID,
JobTitle,
LocationCode,
Department
}
public enum Operator
{
Contains,
StartWith,
EndWith,
EqualTo
}
可能的情况:
Select employee where Name Equals "Value"
Select employee where Name EndWith with "Value"
Select employee where Name StartWith with "Value"
Select employee where JobTitle Contains "Value"
Select employee where Department Equals "Value"
我使用 LINQ 来查询数据库
Employees = from p in DB.Employees
where p.AWCID.Contains(queryvalue)
select p;
但这意味着我必须编写嵌套的 switch case 来覆盖所有情况。 覆盖 3 个变量需要 180 多行代码,
我不太了解 LINQ 语法支持多个值。
有什么想法吗? 谢谢。
I have a a controller action called List that take the following parameters
int Filer, int Field, int Operator, string QueryValue
public enum Filter
{
ActiveEmployee,
OnHoldEmployee,
InactiveEmployee
}
public enum Field
{
Name,
ABRAID,
JobTitle,
LocationCode,
Department
}
public enum Operator
{
Contains,
StartWith,
EndWith,
EqualTo
}
Possible cases:
Select employee where Name Equals "Value"
Select employee where Name EndWith with "Value"
Select employee where Name StartWith with "Value"
Select employee where JobTitle Contains "Value"
Select employee where Department Equals "Value"
I'm using LINQ to query the database
Employees = from p in DB.Employees
where p.AWCID.Contains(queryvalue)
select p;
But thats mean I have to wite nested switch case to cover all cases.
It's over 180 lines of code to cover 3 variables,
I'm not very aware with LINQ syntax to support multiple values.
Any thoughts?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设查询只是一种类型的查询(contains、startswith 等),我会尝试这样的操作:
您还可以将查询类型设置为枚举。
Assuming the query will be only one type of query (contains, startswith, etc.), I would try something like this:
You could also make the query types an enum.
它确实使查询变得复杂。如果使用实体框架,您可以考虑实体 SQL,它的复杂性要低得多,因为您可以将整个查询动态构建为字符串。或者,LINQ to SQL 具有动态 LINQ,它也支持基于文本的查询...
It does complicate the query. If using Entity Framework, you could consider Entity SQL which would be a lot less complex as you can dynamically build the entire query as a string. Or, LINQ to SQL has Dynamic LINQ which also supports textual based queries...