Linq-to-Entities Dynamic sorting

前端 未结 5 661
无人共我
无人共我 2021-02-04 03:51

This is my query, how can I use string as orderby parameter?

string sortColumn=\"Title\";

var  items = (from ltem in ctxModel.Items
              where ltem.Ite         


        
5条回答
  •  时光取名叫无心
    2021-02-04 04:24

    Apparently the other allusions to Dynamic Linq are not clear enough. Let me shed some light..

    Using Dynamic Linq does not necessarily indicate the need for an assembly dependency.

    Dynamic Linq is contained in one source file, if I am not mistaken, that is included in the C# samples everyone should have at least looked at sometime in the last 3 years, and can easily be dropped into a project and namespaced to prevent collisions, thus providing expression building services that can be utilized wherever this need arises.

    I consider the ability to safely construct an expression from a reasonably arbitrary string, which can easily be built on the fly to be the best example of 'dynamic'.

    Consider:

        var query = northwind.Products
                             .Where("CategoryID = 3 AND UnitPrice > 3")
                             .OrderBy("SupplierID");
    

提交回复
热议问题