سؤال
هل هناك طريقة لإنشاء استعلام LINQ لاستخدامه لاحقًا أو لعرضه/طباعته (وبشكل أكثر تحديداً ، الفقرة WREE)؟
المحلول
ما الذي تريده بالضبط؟ يمكنك التقاط فقط التعبير من Where
- شيء مثل:
Expression<Func<SomeType, bool>> predicate = row => row.IsActive
&& row.Color == "red";
لأن هذا ذاهب إلى شجرة تعبير ، هناك معنى ToString()
.
إذا كنت تريد SQL (إلخ) ، فسيكون ذلك محددًا للتنفيذ. على سبيل المثال ، مع LINQ إلى SQL يمكنك استخدامه .Log
- فمثلا، ctx.Log = Console.Out;
إذا كنت تريد أن يخرج المسند من منتصف IQueryable<T>
تغذية ، ثم هذا أصعب بكثير ...
نصائح أخرى
أليس LINQ A.NET 3.5 Feature؟
تعديل:
http://msdn.microsoft.com/en-us/library/bb332048.aspx
-> LINQ متاح فقط من .NET 3.5 ، لذلك ليس 3.0 كما يسأل TopicStarter.
EDIT2:
حسنًا ، يتحدث TS عن C# 3.0 ، والذي يأتي مع .NET 3.5.
مربكة جدا.
يمكنك النظر إلى System.linq.dynamic إنه ملحق إلى LINQ لبناء ديناميكية أين ، orderby إلخ.
مثل tblproduct.where ("Product_id = @0" ، Product_id)
قد يكون ذلك من بعض المساعدة.
انظر إلى هذا المثال من 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));
لا يمكن عرض LINQ (AFAIK) ، إذا كنت تعني استعلام "LINQ2SQL" (QG. SQL QUERY المدمج من LINQ) ، لا ، لا يمكن طباعته.