linq 0 c中的动态连接

   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

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

另一种方法:

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};
https://stackoverflow.com/questions/1263565/dynamic-join-in-linq-0-c-sharp

转载注明原文:linq 0 c中的动态连接