C#检索linq上的where子句

时间:2009-09-10 13:19:18

标签: c# linq c#-3.0

有没有办法构建一个linq查询以便以后使用它或显示/打印它(更具体地说,是where子句)?

5 个答案:

答案 0 :(得分:9)

你到底想要什么?您可以从Where中捕获表达式 - 类似于:

Expression<Func<SomeType, bool>> predicate = row => row.IsActive
          && row.Color == "red";

因为这是一个表达式树,所以有一个有意义的ToString()

如果你想要SQL(等),那么这将是特定于实现的。例如,使用LINQ-to-SQL,您可以使用.Log - 例如,ctx.Log = Console.Out;

如果您希望谓词超出IQueryable<T> Feed的中间位置,那就更难了......

答案 1 :(得分:1)

LINQ不是.NET 3.5功能吗?

修改

http://msdn.microsoft.com/en-us/library/bb332048.aspx

- &GT; LINQ仅在.NET 3.5中提供,因此不会像主题启动者那样提出3.0。

<强> EDIT2:

好的,所以TS谈到了.NET 3.5附带的C#3.0。

相当令人困惑。

答案 2 :(得分:0)

你可以看看System.Linq.Dynamic它是linq的一个插件,用于构建动态where,orderby等。

例如tblProduct.Where(“product_id = @ 0”,product_id)

这可能会有所帮助。

答案 3 :(得分:0)

从MSDN看这个例子:

// Lambda expression as executable code.
Func<int, bool> deleg = i => i < 5;
// Invoke the delegate and display the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));

// Lambda expression as data in the form of an expression tree.
System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
// Compile the expression tree into executable code.
Func<int, bool> deleg2 = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg2(4) = {0}", deleg2(4));

答案 4 :(得分:-1)

Linq无法显示(AFAIK),如果你的意思是'linq2sql'查询(qg。从linq构建的sql查询),不,它无法打印出来。