linq with unassigned variable parameters

前端 未结 3 978
孤独总比滥情好
孤独总比滥情好 2021-01-15 03:10

I have the following variable declarations at the top of my main function

string brand;
double price;
var itemList6 = from i in myStore.items
    where i.pri         


        
相关标签:
3条回答
  • 2021-01-15 03:59

    You could write a function and pass those variables as parameters to avoid intialization..

    Also in case you end up initializing consider using ?? in order to avoid nulls.

    0 讨论(0)
  • 2021-01-15 04:02

    You have to assign a value to the variables before you construct the LINQ query.

    The reason is that the compiler is using the variables to construct the query. Even though the query won't be executed, the compiler requires variables to be assigned before use, and the compiler sees building the query expression as use.

    My recommendation is just to put some nominal default/temp value in there.

    0 讨论(0)
  • 2021-01-15 04:13

    Another way to solve it is to not create the query object until after you have definitely assigned values to those variables.

    0 讨论(0)
提交回复
热议问题