【问题标题】:dynamic join in linq 0 c#动态加入 linq 0 c#
【发布时间】:2009-08-12 07:52:36
【问题描述】:
   var query = from C in db.clients
    join O in db.orders on c.clientid equals O.clientid
    join P in db.products on O.productid equals P.productid
    select new {C,O};

我想根据上述连接执行搜索。输入参数可以是

C.ClientID 和/或 P.ProductName 和/或 P.ProductType 和/或 O.ShippingType

我将如何构建动态搜索子句?

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    嗯,有Dynamic LINQ。这是a nice intro from Scott Gu。使用 Dynamic LINQ,您可以构建条件。例如,

    Where("ClientId = 12")
    

    【讨论】:

      【解决方案2】:

      另一种方法:

      Expression<Func<Client, bool>> clientWhere = c => true;
      Expression<Func<Order, bool>> orderWhere = o => true;
      Expression<Func<Product, bool>> productWhere = p => true;
      
      if (filterByClient)
      {
          clientWhere = c => c.ClientID == searchForClientID;
      }
      
      if (filterByProductName)
      {
          productName = p => p.ProductName == searchForProductNameString;
      }
      
      // other filter cases
      
      var query = from C in db.clients.Where(clientWhere)
          join O in db.orders.Where(orderWhere) on c.clientid equals O.clientid
          join P in db.products.Where(productWhere) on O.productid equals P.productid
          select new {C,O};
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多