【问题标题】:controller method which returns LINQ query result返回 LINQ 查询结果的控制器方法
【发布时间】:2022-01-15 17:39:33
【问题描述】:

我正在创建一个返回LINQ 查询结果的方法。我将其创建为单独的方法,因为多个视图将需要相同的查询。但是我不知道如何创建具有这种返回类型的方法。

public return_type GetData_ById(int? id) // what will be the return type?
        {
            var student_ = (from z in db.StudModels where z.Status == 10 select z).OrderByDescending(z => z.StudId).ToList();
            return student_;
        }
        
        public ActionResult CreateCard(int? id)
        {
            GetData_ById(id);
            return View(student_data);
        }
    

GetData_ById 方法的返回类型是什么,然后如何从CreateCard 方法调用函数?

【问题讨论】:

  • 如果您将鼠标悬停在 student 变量名称上,Visual Studio 将显示类型。

标签: asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您正在 StudModel 上创建一个 LINQ,因此返回类型很可能是一个 StudModel 列表:

List<StudModelsType>

调用函数如下所示:

List<StudModelsType> student_data = GetData_ById(id);
return View(student_data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多