score:7
ToListAsync()
works on a IQueryable<T>
only, when you turned it in to a IEnumerable<T>
via AsEnumerable()
you lost the ability to call it.
Because once you do AsEnumerable()
you are working with a in memory collection anyway, just make it a list at that point.
var allZones = await db.zones.Where(d => d.zone_client_id == "15").ToListAsync().ConfigureAwait(false);
return allZones.Distinct<zones>(new zones.Comparer()).ToList();
The last .ToList()
is optional. If you don't do it and you enumerate the returned IEnumerable multiple times you will run the Distinct
multiple times. Doing a extra .ToList()
makes it so it "remembers" the result of the Distinct
instead of re-calculating it.
score:2
In your GetApptTask method the result at the return line is an IQueryable which has the extension method ToListAsync defined. The call to GetZoneTask has the AsEnumberable which realizes the IQueryable into an IEnumberable which does NOT have the extension method ToListAsync defined.
public async Task<IEnumerable<appointments>> GetApptTask(){
var result = db.appointments.Where(d => d.appt_client_id == 15 && d.customer_id == 68009);
return await result.ToListAsync();//result was an IQueryable here
}
public async Task <IEnumerable<zones>> GetZoneTask()
{
var result = db.zones.Where(d => d.zone_client_id == "15").AsEnumerable().Distinct<zones>(new zones.Comparer());
return await result.ToListAsync();//here result is an IEnumberable because of the above call to AsEnumberable
}
score:4
Because ToListAsync() works on a IQueryable only, when you turned it in to a IEnumerable via AsEnumerable() you lost the ability to call it. You can call .AsQueryable() on an IEnumerable sequence and it will return IQueryable.
For example:
private async Task<List<MyModelType>> GetAsyncListFromIEnumerable(
List<MyModelIEnumerableType> mylist,
DateTime startDate,
DateTime endDate)
{
var result = from e in mylist
where e.StartDate >= startDate
&& e.EndDate <= endDate
select new MyModelIEnumerableType
{
// Set of the properties here
};
return await result.AsQueryable().ToListAsync();
}
score:0
As @Scott Chamberlain mentioned ToListAsync() works with
but you can use
method instead of this.
public async Task<IEnumerable<appointments>> GetApptTask()
{
var result = db.appointments.Where(d => d.appt_client_id == 15 && d.customer_id == 68009);
return await Task.FromResult(result);
}
Source: stackoverflow.com
Related Articles
- Multiple queries in async/await (Error: IEnumerable does not contain ToListAsync())
- IEnumerable Concat Missing, does not contain a definition for 'Concat'
- Why does LINQ to SQL translate GroupBy into multiple queries
- Does this LINQ code perform multiple lookups on the original data?
- System.Linq.IOrderedQueryable' does not contain a definition for 'Skip' error while trying to use Skip method
- C# error CS1061: Type `System.Collections.Generic.List<int>' does not contain a definition for `Length'
- Does LINQ convert code to SQL queries
- How does "group by" work in LINQ? Error "'object' does not contain a definition for 'type'"
- Task Does Not Contain a Definition for Where If Done in One Line of Code
- List or Array of String Contain specific word in Html Source Code
- Error 2 '' does not contain a constructor that takes 1 arguments, (Topic: Typed Context in Linq to SQL )
- Getting error that "'object' does not contain a definition for 'key'" while accessing the key property in Asp.Net
- Object does not contain "fullname" LINQ error
- Sum multiple fields with select new and where raises error 'Model' does not have definition of 'Sum'
- Instantiate a class in MVC 5 gives error [class] does not contain a definition for [member] and no extension method [member] ... could be found
- Async await in linq select
- System.Collections.Generic.List does not contain a definition for 'Select'
- LINQ: Select where object does not contain items from list
- The data source does not support server-side data paging
- Add items to a collection if the collection does NOT already contain it by comparing a property of the items?
- IQueryable<T> does not contain a definition for 'Include' and no extension method 'Include'
- What does this C# code with an "arrow" mean and how is it called?
- System.Linq.IQueryable does not contain a definition for 'Where'
- Resharper: Possible Multiple Enumeration of IEnumerable
- Async await using LINQ ForEach()
- Passing multiple parameters to controller in ASP.NET MVC; also, generating on-the-fly queries in LINQ-to-SQL
- Reading an IEnumerable multiple times
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- IEnumerable<type> does not contain a definition of 'Contains'
- does converting IQueryable to IEnumerable execute the query again?
- LINQ Syntax for Current Revision
- Returning a nullable DateTime in LINQ2SQL
- problem with linq (C#)
- How to combine node values from different nodes into one node separated with a "/" every 2 values
- c# XML and LINQ
- Sort an XML preserving whitespace/indentation?
- Logic to decipher consecutive date ranges
- XML elements to Tuple using LINQ?
- Change value of EditorFor on DropDownFor value change
- Create a class type in code in .net c#
- Code review of this portion of Linq Filters
- Please explain this behaviour (is LINQ cached)?
- What more LINQ operators/methods can I use to optimize my C# Collections based code?
- Is there any performance impact of calling ToList() multiple times?
- LINQ-to-SQL: Select mulitple properties from top row of each GROUP using LINQ query
- The specified type member 'Title' is not supported in LINQ to Entities
- Using LINQ with wildcard characters without using SqlMethods
- Controller Where clause - how to select where not like
- Conversion from Int array to string array
- Cannot compare in linq