gpt4 book ai didi

c# - EF5 IQueryable 不包含 ToListAsync 的定义

转载 作者:行者123 更新时间:2023-12-05 07:31:43 26 4
gpt4 key购买 nike

我有这段代码,但我不知道我的代码有什么问题。我有 EF 5,我想创建一个 Task await 方法。

我想做的是以异步方式选择查询一个简单的表。但是 ToListAsync() 方法似乎不在我的 EF 中。

我该如何解决这个问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EO;
using System.Data.Objects;
using System.Data.Objects.SqlClient;
using System.Transactions;
using System.Threading;
using System.Data.Entity;

namespace DimexCEDatos
{
static public class AfiliadosDatos
{
static List<myclass> lstSegmentos = new List<myclass>();

public static async Task<List<myclass>> myfunction()
{
try
{
using (MyDB db = new MyDB())
{
lstSegmentos = await (from item in db.genAfiliados
select item).AsQueryable().ToListAsync();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

return lstSegmentos;
}
}
}

最佳答案

我不是很确定,但也许你可以使用下面的扩展方法

public static class SampleExtension
{
public static async Task<List<T>> ToListAsync<T> (this IQueryable<T> obj) => await Task.Run(() => obj.ToList());
}

关于c# - EF5 IQueryable 不包含 ToListAsync 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51659659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com