IEnumerable<电影>不包含 FirstOrDefaultAsync() 的定义 [英] IEnumerable&lt;Movies&gt; does not contain definition for FirstOrDefaultAsync()

查看:37
本文介绍了IEnumerable<电影>不包含 FirstOrDefaultAsync() 的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的错误信息:IEnumerable"不包含FirstOrDefaultAsync"的定义,并且找不到接受IEnumerable"类型的第一个参数的可访问扩展方法FirstOrDefaultAsync"(您是否缺少 using 指令或程序集引用?)

Here is my error message: 'IEnumerable' does not contain a definition for 'FirstOrDefaultAsync' and no accessible extension method 'FirstOrDefaultAsync' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

我已执行以下操作,但错误仍然存​​在:

I have done the following and the error persists:

  1. 我没有忘记使用 await,根据这个链接:任务不包含FirstOrDefault"的定义

我引用了 Sytem.Core 并导入了 System.Linq 和 System.Data.Entity.(System.Linq 不明确/灰色,因此未使用),根据此链接:DbSet 不包含 FirstOrDefault 的定义?

I referenced Sytem.Core and imported both System.Linq and System.Data.Entity.(System.Linq is ambiguous/grey so it is not being used), as per this link: DbSet doesn't contain definition for FirstOrDefault?

这是我的 .cshtml.cs 代码,其中正在生成错误(第 34 行):

Here's my .cshtml.cs Code, where the error is being generated (line 34):

using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Movies.Models;

namespace Movies.Pages.MediaDetails
{
    public class DetailsModel : PageModel
    {
        private readonly Movies.Models.MoviesContext _context;

        public DetailsModel(Movies.Models.MoviesContext context)
        {
            _context = context;
        }

        MovieDataAccessLayer objmovie = new MovieDataAccessLayer();

        public Movies Movies { get; set; }

        public async Task<IActionResult> OnGetAsync(byte? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Movies = await objmovie.MovieByMedia(id).FirstOrDefaultAsync(m => m.MediaTypeID == id);

            if (Movies == null)
            {
                return NotFound();
            }
            return Page();
        }
    }
}

这是我的 DataAccessLayer,它被上面的代码引用并返回 IEnumerable.

Here's my DataAccessLayer, which is being reference by the code above and returns IEnumerable.

using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Movies.Models
{
    public class MovieDataAccessLayer
    {
        string connectionString = "Server=servername;Database=DatabaseMovies;Trusted_Connection=True;MultipleActiveResultSets=true";

        public IEnumerable<Movies> MovieByMedia(byte? MediaID)
        {
            List<Movies> lstmovie = new List<Movies>();

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("usp_MovieByMedia", con);
                cmd.Parameters.Add(new SqlParameter("@MediaID", SqlDbType.TinyInt) { Value = MediaID });
                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    Movies movies = new Movies();

                    movies.MovieTitle = rdr["MovieTitle"].ToString();
                    movies.MovieYear = Convert.ToInt32(rdr["MovieYear"]);

                    lstmovie.Add(movies);
                }

                con.Close();

            }
            return lstmovie;
        }

    }
}

任何输入将不胜感激.

推荐答案

.FirstOrDefaultAsync() 方法是 IQueryable 接口上的扩展方法.由于您的数据访问方法返回 IEnumerable 并且不是异步的,因此您应该使用 .FirstOrDefault().

The .FirstOrDefaultAsync() method is an extension method on the IQueryable interface. Since your data access method returns an IEnumerable and isn't asynchronous, you should be using .FirstOrDefault().

https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.firstordefaultasync?view=entity-framework-6.2.0

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=netframework-4.8

这篇关于IEnumerable<电影>不包含 FirstOrDefaultAsync() 的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆