Share via


Can i use TolistAsync() when doing LINQ to object

Question

Monday, April 13, 2020 7:24 AM

 var list10Qk = dt10QkBogey.AsEnumerable()
                    .Select(row => new PeriodicalData
                    {
                        Section = (row.Field<string>("TabName")).ToString(),
                        LineItem = (row.Field<string>("StandardLineItem")).ToString(),
                        XFundCode = (row.Field<string>("XFundCode")).ToString(),
                        StandardDate = (row.Field<string>("StandardDate")).ToString(),
                        StandardValue = (row.Field<string>("StandardValue")).ToString()
                    }).ToList();

here is a code snippet and this way i populate my list using ToList(). so i want to know can i use TolistAsync() to populate my list instead of TolIst()?

now i am using dotnet v4.5.2. so tell me in which version TolistAsync() available ?

when to use TolistAsync() ?

thanks

All replies (6)

Monday, April 13, 2020 10:16 AM ✅Answered

Hello,

If you are simply looking to keep the app responsive you can inject a delay in a for-next rather than use Lambda as you are now. Lambda or LINQ are not the answer to everything nor is ToListAsync which as Timon pointed out is for.

The following is a pattern to follow were we have a count of records in the DataTable, here it's fixed set in the variable recordCountDataTable. The method is returning a list.

private async void button1_Click(object sender, EventArgs e)
{
    var list = await Example();
}

private async Task<List<PeriodicalData>> Example()
{
    var recordCountDataTable = 120;
    var periodicalList = new List<PeriodicalData>();

    for (int rowIndex = 0; rowIndex < recordCountDataTable; rowIndex++)
    {

        var percentage = (rowIndex * recordCountDataTable) / 100;

        /*
         * inject a delay on each 25% done
         * The percentage can change e.g. every 10 percent for instance
         * The delay can change to but higher numbers will delay a good deal
         */
        if (percentage % 25 == 0 && percentage >0)
        {
            await Task.Delay(1);
            // here is were you add to your list
            periodicalList.Add(new PeriodicalData());
        }
    }

    return periodicalList;
}

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Monday, April 13, 2020 4:54 PM ✅Answered

i am looking for few good usage of ToListAsync() but it was missing in your code.

It's missing in my code because I've shown an alternate idea.

It's for Entity Framework, use Entity Framework if you want to find good usages for it.

/en-us/dotnet/api/microsoft.entityframeworkcore.entityframeworkqueryableextensions.tolistasync?view=efcore-3.1

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Tuesday, April 14, 2020 5:11 AM ✅Answered

Why do you want ToListAsync?  Async processing is only useful if you have other things you can do while you wait.

An excellent general rule is "first make it work, then make it fast."  Do you, in fact, know that your program is not fast enough?

Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.


Monday, April 13, 2020 9:09 AM

Hi Sudip_inn,

Thank you for posting here.

TolistAsync () is a function of Entity Framework.

Looking at this post, he introduced when it is best not to use TolistAsync ().

Entity Framework async operation takes ten times as long to complete

As for when it is suitable to use, it is difficult to say, whether the overhead saved by using asynchronous can offset the overhead of asynchronous itself depends on different situations, there is no definitive answer. 

You need to try it separately before deciding which method to use.

Best Regards,

Timon

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Monday, April 13, 2020 4:04 PM

so are you trying to say i should not use ToListAsync() when will use EF ?


Monday, April 13, 2020 4:06 PM

i am looking for few good usage of ToListAsync() but it was missing in your code.