Yield return database records using LinqToSql?

后端 未结 3 2015
长发绾君心
长发绾君心 2021-01-01 03:17

I have a supplying method in DAL:

public IEnumerable GetRecords()
{
    using (LinqDataContext context = new LinqDataContext())
    {
              


        
3条回答
  •  离开以前
    2021-01-01 03:47

    1) yield will not necessarily be faster to get all values, but it would allow the code to start handling results before the database has returned all results. That is, yield returns the first result the instant it shows up, while ToArray() needs to wait for all results to show up before returning. Of course, if the underlying providers return all results at once due to buffering or other reasons, this may not make a difference.

    2) Yes, using will dispose of the LinqDataContext no matter how you exit the using block (exceptions/return/break/...)

提交回复
热议问题