看不到FirstOrDefault

use*_*239 2 c# linq

任何帮助都非常感谢大家!

在GetProductByID下,我收到一条错误,说"int不包含FirstOrDefault的定义".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HomebaseSystemNew.Models;

namespace HomebaseSystemNew.Controllers
  {
    public class ProductRepository
   {

    public ProductsController GetProductID(int Pid)
        {
          Product db = new Product();

          return db.Pid.FirstOrDefault(db => db.Pid == Pid);
        }


   }




   }
Run Code Online (Sandbox Code Playgroud)

max*_*pan 13

此外,您需要使用System.Linq命名空间添加以访问这些方法.


Ode*_*ded 0

从您的代码看来,它db.Pid是一个整数(Int32),而不是IEnumerable<T>.

运算LINQ符是在 上定义的IEnumerable,而不是在 上定义的Int32

默认情况下它将是0.