How do I reference a field in Linq based on a dynamic fieldname

后端 未结 5 1325
予麋鹿
予麋鹿 2021-01-05 08:22

Firstly, apologies for the bad question title - not entirely sure if I am asking the correct thing.

Normally I can do the following to access a field:



        
5条回答
  •  执笔经年
    2021-01-05 08:43

    Weirdly I have just been reading something similar on Scott Hanselman's blog, this is to set the where or ordering by a field name in a string but I think the select could be done in the same way. See:

    http://www.hanselman.com/blog/TheWeeklySourceCode48DynamicQueryableMakesCustomLINQExpressionsEasier.aspx

    The core being something like :

    Dim Northwind As new NorthwindDataContext
    Dim query = Northwind.Products
            .Where("CategoryID=2 And UnitPrice>3")
            .OrderBy("SupplierID")
    GridView1.DataSource = query
    GridView1.DataBind()
    

    It may require some dynamic data references.

提交回复
热议问题