I would like to share that I went through other similar posts but the suggested solutions din't work for me and that's why I am creating a separate thread.I am trying to join the two results using Asynchronous programming in Entity Framework.I have three methods as below:
The method PreprocessAppointment() waits on other two methods GetApptTask() and GetZoneTask()
public async void PreprocessAppointment()
{
var task_1 = GetApptTask();
var task_2 = GetZoneTask();
await Task.WhenAll(task_1, task_2);
}
The method GetApptTask() does not give any error.
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();
}
The method GetZoneTask()gives the following error. IEnumerable <zones> does not contain definition for ToListAsync().
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();
}
I am unable to find out what might be causing this error.I am attaching the models structures as well for appointments and zones below. The only difference between the models apart from fields is the definition of Comparer class in zones.
zones.cs
public class zones
{
[Column(Order=0),Key]
[StringLength(50)]
public string zone_client_id { get; set; }
//public int zone_client_id { get; set; }
[Column(Order = 1), Key]
[StringLength(5)]
public string zip { get; set; }
[StringLength(50)]
public string zone { get; set; }
public class Comparer : IEqualityComparer<zones>
{
public bool Equals(zones x, zones y)
{
return x.zone_client_id == y.zone_client_id
&& x.zip == y.zip
&& x.zone == y.zone;
}
public int GetHashCode(zones obj)
{
return obj.zone_client_id.GetHashCode() ^
obj.zip.GetHashCode() ^
obj.zone.GetHashCode();
}
}
}
appointments.cs
public partial class appointments
{
public int appt_client_id { get; set; }
public int customer_id { get; set; }
[Key]
public int appt_id { get; set; }
public DateTime appt_date_time { get; set; }
[StringLength(200)]
public string recording_uri { get; set; }
public DateTime time_stamp { get; set; }
[Required]
[StringLength(20)]
public string appt_status { get; set; }
[Required]
[StringLength(5)]
public string appt_type { get; set; }
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With