How to check IEnumerable returns null or has any row?

后端 未结 1 1317
既然无缘
既然无缘 2021-01-19 14:41

i have a linq query similar like below.

IEnumerable query= (from item in IItemsTable.AsEnumerable()
                         where someconditi         


        
相关标签:
1条回答
  • 2021-01-19 15:09

    You can use the extension method Any():

    if(query.Any())
    {
        //query has results.
    }
    

    Note that if you only care whether there are rows or not (and don't subsequently do something with those rows) you can use another overload of Any() to do it in one line:

    bool queryhasresults = IItemsTable.AsEnumerable().Any(item => somecondition);
    
    0 讨论(0)
提交回复
热议问题