字符串到linq表达式?[英] String to LINQ expression?

本文是小编为大家收集整理的关于字符串到linq表达式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何将字符串与查询中的字符串转换为实际的linq表达式,以便我执行它?

if(email!= null)
{
    query += "x => x.Email.Contains(email)";
}
if (firstname != null)
{
    query += "&& x.FirstName.Contains(firstname)";
}
if (lastname != null)
{
    query += "&& x.LastName.Contains(lastname)";
}
return context.UserAccounts.Where(query).ToList();

推荐答案

尝试以下方法:

<your collection>.
    .Where(o => (email!= null)? o.Email.Contains(email) : true).
    .Where(o => (firstname != null)? o.FirstName.Contains(firstname) : true).
    .Where(o => (lastname != null)? o.LastName.Contains(lastname) : true).
    ToList();

基本上意味着只有在满足条件时才应用过滤器(就像在您的句子中一样).

其他推荐答案

而不是将查询作为字符串保存,而是可以将其保存为IQueryable(System.Linq命名空间).

var query = context.UserAccounts;
if(email!= null)
{
    query = query.Where(x => x.Email.Contains(email));
}

if (firstname != null)
{
    query = query.Where(x => x.FirstName.Contains(firstname));
}

if (lastname != null)
{
    query = query.Where(x => x.LastName.Contains(lastname));
}
return query.toList();

当您最终调用ToList() IQueryable被列举为UserAccount的List.

其他推荐答案

如果需要查询,则需要查询条款,则每次都需要将结果保存在查询本身中

var query = context.UserAccounts;

if(email != null)
{
    query = query.Where(x => x.Email.Contains(email));
}
if (firstname != null)
{
    query = query.Where(x => x.FirstName.Contains(firstname));
}
if (lastname != null))
{
    query = query.Where(x => x.LastName.Contains(lastname));
}
return query.ToList();

另一种复杂方法可以是使用动作

本文地址:https://www.itbaoku.cn/post/1557225.html

问题描述

How can I transform the string with the query in it into an actual LINQ expression so I can execute it?

if(email!= null)
{
    query += "x => x.Email.Contains(email)";
}
if (firstname != null)
{
    query += "&& x.FirstName.Contains(firstname)";
}
if (lastname != null)
{
    query += "&& x.LastName.Contains(lastname)";
}
return context.UserAccounts.Where(query).ToList();

推荐答案

Try this:

<your collection>.
    .Where(o => (email!= null)? o.Email.Contains(email) : true).
    .Where(o => (firstname != null)? o.FirstName.Contains(firstname) : true).
    .Where(o => (lastname != null)? o.LastName.Contains(lastname) : true).
    ToList();

Basically it means the filter will apply only when the condition is met (just like in your if sentences).

其他推荐答案

Instead of saving your query as a string you could save it as an IQueryable (from the System.Linq namespace).

var query = context.UserAccounts;
if(email!= null)
{
    query = query.Where(x => x.Email.Contains(email));
}

if (firstname != null)
{
    query = query.Where(x => x.FirstName.Contains(firstname));
}

if (lastname != null)
{
    query = query.Where(x => x.LastName.Contains(lastname));
}
return query.toList();

When you finally call ToList() the IQueryable is enumerated to a List of UserAccount.

其他推荐答案

If the query is required with the combination of where clauses then every time result needs to be saved in query itself

var query = context.UserAccounts;

if(email != null)
{
    query = query.Where(x => x.Email.Contains(email));
}
if (firstname != null)
{
    query = query.Where(x => x.FirstName.Contains(firstname));
}
if (lastname != null))
{
    query = query.Where(x => x.LastName.Contains(lastname));
}
return query.ToList();

Another complex approach could be to use Actions