Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The query results enumerated more than once

    datacontextclass dc=new datacontextclass ();
       var news= dc.GetNewsCompany(Int64.Parse ( _idCompany));
       if (news.GetEnumerator().MoveNext())
       {
           foreach (var item in news)
           {
               drpListNews.Items.Add(item.Title);
           } 
       }

return error:{"The query results cannot be enumerated more than once."}

how can check result != null in LINQ;

like image 894
ashkufaraz Avatar asked Jan 28 '26 16:01

ashkufaraz


1 Answers

Using an enumerator wildly is a bad idea - for example, it needs disposing - which you haven't done (this could lead to a SqlDataReader being left open - not good). In this case, just enumerate it. If there aren't any records, that will be trivial:

   if (news!=null)
   {
       foreach (var item in news)
       {
           drpListNews.Items.Add(item.Title);
       } 
   }

If you need the data twice, put it in a list:

   var news = (blah).ToList();
like image 128
Marc Gravell Avatar answered Jan 31 '26 06:01

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!