代码之家  ›  专栏  ›  技术社区  ›  Toto

C检索LINQ上的WHERE子句

  •  3
  • Toto  · 技术社区  · 15 年前

    有没有一种方法可以构建一个LINQ查询以便稍后使用,或者显示/打印它(更具体地说,是WHERE子句)?

    5 回复  |  直到 15 年前
        1
  •  9
  •   Marc Gravell    15 年前

    你到底想要什么?你可以捕捉到 表达 Where -比如:

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

    因为这是一个表达式树,所以 ToString() .

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

    如果希望谓词不在 IQueryable<T> 喂,那就更难了…

        2
  •  1
  •   Natrium    15 年前

    Linq不是.NET 3.5功能吗?

    编辑:

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

    -->Linq只能从.NET 3.5中获得,因此不能按照TopicStarter的要求提供3.0。

    编辑2:

    好的,所以TS讨论C 3.0,它与.NET 3.5一起提供。

    相当混乱。

        3
  •  0
  •   Matthew Hood    15 年前

    您可以查看system.linq.dynamic,它是linq的一个插件,用于构建动态where、orderby等。

    例如tblproduct.where(“product_id=@0”,product_id)

    这可能会有所帮助。

        4
  •  0
  •   Dewfy    15 年前

    从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));
    
        5
  •  -1
  •   nothrow    15 年前

    如果您的意思是“linq2sql”查询(qg),则无法显示linq(afaik)。从Linq生成的SQL查询),不,无法打印出来。