2009-09-10 63 views

回答

9

你究竟想要什么?您可以从Where捕捉只是表达 - 是这样的:因为这将表达式树

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

,有一个有意义的ToString()

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

如果你想谓词出IQueryable<T>料的中间,那么这是非常棘手......

+0

我可以使用表达式作为where子句吗?例如:IEnumerable users = from GetAllUsers()其中myExpression选择你。用我的表达方式表达>? – Toto 2009-09-10 13:35:38

+0

是的,但是作为'... = GetAllUsers()。Where(myExpression)...' – 2009-09-10 13:38:05

+0

请注意,如果'GetAllUsers()'返回'IEnumerable ',则需要使用'.AsQueryable()。 (myExpression)' – 2009-09-10 13:38:42

-1

Linq无法显示(AFAIK),如果您的意思是'linq2sql'查询(qg.sql查询由linq构建),否则不能打印出来。

+0

myDataContext.GetCommand(查询).CommandText? – 2009-09-10 13:28:13

+0

@大卫B:mea culpa。感谢纠正,我不知道这一点。 – nothrow 2009-09-10 13:35:25

1

LINQ不是.NET 3.5特性吗?

编辑:

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

- > LINQ只能从.NET 3.5,所以不是3.0的topicstarter要求。

EDIT2:

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

很混乱。

+1

标记用于C#3.0,这是.NET 3.5附带的C#版本。不过,你不应该对这个国际海事组织有负面评价。 – 2009-09-10 13:28:07

+1

C#中的LINQ语法是C#3.0功能。 LINQ实现是.NET 3.5的特性。 C#3.0与.NET 3.5同时发布 – 2009-09-10 13:29:11

+1

@Petar,这些评论应该作为评论,而不是作为答案。它不会帮助主题启动器,它只会添加噪声,而且它的意思是...... – Razzie 2009-09-10 13:41:39

0

你可以看System.Linq.Dynamic它的一个插件,以LINQ建立动态,其中,排序依据等

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

这可能会有一些帮助。

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));