【问题标题】:How to get Specific Columns from Sql server in Asp.Net Core MVC [duplicate]如何从 Asp.Net Core MVC 中的 Sql 服务器获取特定列 [重复]
【发布时间】:2020-08-15 17:15:52
【问题描述】:

AppDbContext.cs

using Microsoft.EntityFrameworkCore;

namespace EduManSystems.Model
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            : base(options)
        {

        }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Student> Student_master { get; set; }
    }
}

IStudentRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EduManSystems.Model
{
    public interface IStudentRepository
    {
        Student GetStudent(string id);
        IEnumerable<Student> GetStudents();
        //Student GetStudents();
        Student Add(Student student);
    }
}

SqlStudentRepository.cs

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.Common;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace EduManSystems.Model
{
    public class SqlStudentRepository : IStudentRepository
    {
        private readonly AppDbContext context;

        public SqlStudentRepository(AppDbContext context)
        {
            this.context = context;
        }

        public Student GetStudent(string id)
        {
            return context.Student_master.Find(id);
        }

        public IEnumerable<Student> GetStudents()
        {
            return context.Student_master.FromSql("select * from student_master").ToList();
        }
     }
}

StudentController.cs

using EduManSystems.Model;
using EduManSystems.ViewModel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace EduManSystems.Controllers
{
    public class StudentController : Controller
    {
        private readonly IStudentRepository _studentRepository;
        private readonly IHostingEnvironment hostingEnvironment;

        public StudentController(IStudentRepository studentRepository,
                                    IHostingEnvironment hostingEnvironment)
        {
            _studentRepository = studentRepository;
            this.hostingEnvironment = hostingEnvironment;
        }

       [HttpGet]
        public ViewResult Index()
        {
            var model = _studentRepository.GetStudents();
            return View(model);
        }

       [HttpGet]
        public JsonResult StudentList()
        {
            var model = _studentRepository.GetStudents();
            return Json(model);
        }
    }
}

我试过这个,但我得到了这样的错误,

大家好,

我需要你的帮助。在这里,它从 SQL 服务器(原始 sql 查询或 Linq)中检索所有数据。但是,我只需要特定的列,例如 select stud_first_name,stud_last_name from student_master;

SQL 查询必须返回表的所有列。例如context.Student_Master.FromSql("Select Stud_First_Name, Stud_Last_Name from Student_Master).ToList() 会抛出异常。

请帮帮我

谢谢,

【问题讨论】:

  • 你也可以添加Student类代码。

标签: c# sql asp.net-mvc linq


【解决方案1】:

使用下面的示例获取实体的属性名称:

 var names = typeof(User).GetProperties()
                            .Select(property => property.Name).ToArray();

【讨论】:

    【解决方案2】:

    首先,如果你已经正确映射了你的db结构,你不需要.FromSql,只需ToList就足够了,其次你可以做context.Student_master.Select(s=&gt; ...).ToList()

    更新

    来自评论:

    您需要引入只包含所需字段子集的新类,或者(错误的选择)做.Select(s =&gt; new Student { Stud_First_Name = s.Stud_First_Name , Stud_Last_Name = s.Stud_Last_Name })

    【讨论】:

    • 我已经试过这个了。但是,我得到了类似匿名类型的返回错误(无法隐式转换......)
    • 是的,因为您的IEnumerable&lt;Student&gt; GetStudents() 应该返回Student 的可枚举值。因此,要么您引入仅包含所需字段子集的新类,要么(错误选项)执行.Select(s =&gt; new Student { Stud_First_Name = s.Stud_First_Name , Stud_Last_Name = s.Stud_Last_Name })
    • 兄弟,它工作正常.... .Select(s => new Student { Stud_First_Name = s.Stud_First_Name , Stud_Last_Name = s.Stud_Last_Name })....非常感谢
    • 兄弟,mark 将在 24 小时后接受。
    【解决方案3】:
    public IEnumerable<Student> GetStudents()
            {
                return context.Student_master.Select(s => new Student { Stud_First_Name = s.Stud_First_Name, Stud_Last_Name = s.Stud_Last_Name });
            }
    

    现在 View 和 Json 都可以正常工作了

    非常感谢Guru Stron.....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2016-11-05
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多