dynamic join in linq 0 c#

前端 未结 2 1584
面向向阳花
面向向阳花 2020-12-11 14:06
   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 ne         


        
相关标签:
2条回答
  • 2020-12-11 14:37

    Another method:

    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};
    
    0 讨论(0)
  • 2020-12-11 14:46

    Well, there's Dynamic LINQ. Here's a nice intro from Scott Gu. With Dynamic LINQ, you can build your conditionals. For example,

    Where("ClientId = 12")
    
    0 讨论(0)
提交回复
热议问题