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> clientWhere = c => true; Expression> orderWhere = o => true; Expression> 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}; 

好吧,有动态LINQ 。 这是Scott Gu的一个很好的介绍 。 使用Dynamic LINQ,您可以构建条件。 例如,

 Where("ClientId = 12")