本文介绍了异步/等待中的多个查询(错误:IEnumerable不包含ToListAsync())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享我经历过其他类似的帖子,但是建议的解决方案对我不起作用,这就是为什么我创建一个单独的线程.我试图在Entity Framework中使用异步编程将这两个结果结合在一起.我有以下三种方法:

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:

方法 PreprocessAppointment()等待其他两个方法 GetApptTask() GetZoneTask()

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);
        }

方法 GetApptTask()没有给出任何错误.

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();


        }

方法 GetZoneTask()出现以下错误. IEnumerable< zones>不包含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();
        }

我无法找出导致此错误的原因.我还为以下约会区域附加了模型结构.除字段外,模型之间的唯一区别是在 zones 中的 Comparer 类的定义.

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() 仅在通过 AsEnumerable()将其变成 IEnumerable< T> IQueryable< T> 上起作用您失去了调用它的能力.

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.

因为一旦执行 AsEnumerable()后,无论如何您都在使用内存中的集合,因此只需将其作为列表即可.

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();

最后一个 .ToList()是可选的.如果不这样做,并且多次枚举返回的IEnumerable,则将多次运行 Distinct .进行额外的 .ToList()可以使它记住" Distinct 的结果,而不是重新计算.

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.

这篇关于异步/等待中的多个查询(错误:IEnumerable不包含ToListAsync())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 05:44