是否可以使用字符串作为 LINQ 查询表达式?

发布于 2024-11-26 15:29:56 字数 554 浏览 0 评论 0原文

如果某些变量具有某些值,我需要提取一些记录。

例如,如果 status>0 我需要像这样过滤结果:

where object.id=status

否则,如果 status=0,我需要删除此 where 子句并返回所有元素。我将摆脱:

if(status>0)
   do a linq query with the where clauses
else
   do a link query with that where clauses

太多的代码,因为要检查的变量可能超过 4-5 个。

是否可以在 LINQ 上“注入”某种字符串? (这样我就可以创建字符串并将其传递给 LINQ)。

我的意思是:

string myQuery="";
if(status>0)
   myQuery="where object.id=status";
else
   myQuery="";

有可能吗? (经典的 mysql 行为)。

I need to extract some records if some variables have some values.

For example, if status>0 I need to filter result like :

where object.id=status

else, if status=0, I need to remove this where clauses and return all elements. I'll get rid about :

if(status>0)
   do a linq query with the where clauses
else
   do a link query with that where clauses

too much code, because the variables to check could be more than 4-5.

Is it possible to "inject" a sort of string on LINQ? (So i can create my string and pass it to the LINQ).

I mean somethings like :

string myQuery="";
if(status>0)
   myQuery="where object.id=status";
else
   myQuery="";

is it possible? (Classic mysql behaviour).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

帥小哥 2024-12-03 15:29:56

由于 LINQ 很懒,所以你可以这样做

var query = ...

if (status > 0)
{
    query = query.Where(o => o.id == status);
}

Since LINQ is lazy, you can just do

var query = ...

if (status > 0)
{
    query = query.Where(o => o.id == status);
}
栖迟 2024-12-03 15:29:56

您可以建立这样的查询:

IEnumerable<MyEntity> results = MyEntityContext.MyEntities;
if (status > 0)
    results = results.Where(e => e.id == status);

这有帮助吗?

You can build up a query like this:

IEnumerable<MyEntity> results = MyEntityContext.MyEntities;
if (status > 0)
    results = results.Where(e => e.id == status);

Does that help?

べ映画 2024-12-03 15:29:56

可以使用动态 LINQ,请参阅 ScottGu 的博客文章:
动态 LINQ(第 1 部分:使用 LINQ 动态查询库)

在此输入图像描述

It is possible using Dynamic LINQ, see ScottGu's blog post:
Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

enter image description here

恬淡成诗 2024-12-03 15:29:56

您可以这样做:

  var query = database.MyTable.Where(/* where for all records */);

  if (status > 0) {
      query = query.Where(o => o.id == status);
  }

Linq(到 sql 和 EF)足够智能,可以合并 where 条件并将一个 SQL 语句发送到数据库。

You could do it like this:

  var query = database.MyTable.Where(/* where for all records */);

  if (status > 0) {
      query = query.Where(o => o.id == status);
  }

Linq (to sql and EF) is smart enough to merge the where conditions and send one SQL statement to the database.

很酷不放纵 2024-12-03 15:29:56

可以使用动态 linq - 请参阅如何从字符串创建 LINQ 查询?

我的答案链接到 Scott Gu 的帖子和 Microsoft 的示例代码 - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

It is possible using dynamic linq - see How to create LINQ Query from string?

My answer there links to Scott Gu's posts and the sample code from Microsoft - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

画▽骨i 2024-12-03 15:29:56

AFAIK,您将需要有两个不同的查询。

if(status > 0)
{
    var myquery = From ....
    where object.id = status
}
else
{
    var myquery = From ..
}

AFAIK, you will need to have 2 different queries.

if(status > 0)
{
    var myquery = From ....
    where object.id = status
}
else
{
    var myquery = From ..
}
凹づ凸ル 2024-12-03 15:29:56

另一种选择:query.Where(x=>(status>0? x.id==status : 1==1))

Another option: query.Where(x=>(status>0? x.id==status : 1==1))

木落 2024-12-03 15:29:56

您是否正在尝试执行条件 LINQ 查询?如果是这样也许这会有所帮助

var nums = new List<int>() { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
bool getOdd = true;

var query = nums.AsQueryable();

if (getOdd) {
    query = query.Where(i => i == 1 || i == 3);
} else {
    query = query.Where(i => i == 2 || i == 4);
}

var result = query.ToList();

Are you attempting to do a conditional LINQ query? if so maybe this would help

var nums = new List<int>() { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
bool getOdd = true;

var query = nums.AsQueryable();

if (getOdd) {
    query = query.Where(i => i == 1 || i == 3);
} else {
    query = query.Where(i => i == 2 || i == 4);
}

var result = query.ToList();
一个人的旅程 2024-12-03 15:29:56

您可以编写以下内容

IQueryable<T> query = dbContext.SomeObjectSet;

if (condition1) {
    query = query.Where(...)
}

if (condition2) {
    query = query.Where(...)
}

但是,您想要查询所有实体,您可以随后使用 LINQ to SQL 在内存中进行过滤

You can write the following

IQueryable<T> query = dbContext.SomeObjectSet;

if (condition1) {
    query = query.Where(...)
}

if (condition2) {
    query = query.Where(...)
}

However, you you want to query all entities, you can filter in memory afterwards using LINQ to SQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文