我在LINQ查询中收到一条错误消息 [英] I receive an error message in LINQ query

查看:72
本文介绍了我在LINQ查询中收到一条错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC应用程序中有一个Action Method,它使用LINQ查询从数据库表StudentsInfo中检索Student列表。当我从表中检索所有列然后查询工作,但当我尝试从表中检索一些特定的列,然后我收到以下错误消息:



< blockquote class =quote>

Quote:

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [<> f__AnonymousType6`1 [ System.String]]',但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable`1 [MVCApplication.StudentsInfo]'的模型项。





我的尝试:



以下是行动方法



 public ActionResult GetAllStudents()
{
//从StudentsInfo表中检索数据
var result = from db inSt.StudentsInfo
select new
{
d.StdRegNumber,
d.StdName,
d.StdDoBirth
};


return View(result.ToList());
}

解决方案

当您使用new {...}语法时,您正在创建所谓的匿名语言类型,看起来像临时类型或即时创建的类型。匿名类型只能在创建它们的函数中使用,因此您无法将该结果传递给View。



如果您想减少列数然后创建一个名为StudentViewModel的新具体类,它具有你想要的三个字段,然后将代码改为类似



选择新的StudentViewModel 
{
StdRegNumber = d.StdRegNumber,
StdName = d.StdName,
StdDoBirth = d.StdDoBirth
};





您还需要更改视图以使用IEnumerable的模型<< StudentViewModel>



或您可以将原始的studentinfo列表传递给视图,只需忽略您不感兴趣的字段。


如F-ES Sitecore所述,当您使用<$ c $时c> new 关键字,您的返回类型将转换为匿名类型。您收到错误是因为您的查看需要强类型模型 IEnumerable< StudentsInfo> 但是你回复了一个匿名类型。



为了解决这个问题,你可以返回动态类型或 ExpandoObject 如本文所示:在ASP.NET MVC中使用匿名类型集合绑定视图 - DotNetFunda.com [ ^ ]



Keep记住,当进入匿名动态类型时,你将失去强类型的所有好东西具有。 (例如,查看的编译时检查和智能感知)。所以想一想。在运行时,控制器最终可能会将任何类型传递给 View 。我们甚至无法分析代码和猜测,因为动作过滤器可能会更改传递给View的对象我们所知道的全部。



我的建议是使用强类型查看并返回一个具体模型,该模型仅包含 View <中所需的字段/ code>就像F-ES Sitecore已经建议的那样。


I have an Action Method in MVC application that retrieves the list of Students from a database table StudentsInfo using LINQ query. When i retrieve all the columns from the table then the Query works but when i try to retrieve some specific columns from the table then i get the following error message:

Quote:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType6`1[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVCApplication.StudentsInfo]'.



What I have tried:

Following is the action Method

public ActionResult GetAllStudents()
        {
            //Retrieve data from StudentsInfo table
           var result = from d in db.StudentsInfo
                        select new
                        {
                            d.StdRegNumber,
                           d.StdName,
                           d.StdDoBirth
                        };

           
            return View(result.ToList());
        }

解决方案

When you use "new { ... }" syntax you're creating what's known as an anonymous type, look on at like a temporary type or a type created on-the-fly. Anonymous types can only be used inside the function they are created in, so you can't pass that result to a View.

If you want a reduced number of columns then create a new concrete class called something like StudentViewModel that has the three fields you want, then change the code to something like

select new StudentViewModel
{
    StdRegNumber = d.StdRegNumber,
    StdName = d.StdName,
    StdDoBirth = d.StdDoBirth
};



You'll also need to change the view to use a model of IEnumerable<<StudentViewModel>

Or you could just pass the original studentsinfo list to the view and simply ignore the fields you're not interested in.


As stated by F-ES Sitecore , when you use the new keyword, your return type will be converted to anonymous type. You are getting an error because your View expects a strongly-type model IEnumerable<StudentsInfo> but you were returning an anonymous type.

In order to fix that, you could return a dynamic type or ExpandoObject as what is demonstrated in this article: Binding views with Anonymous type collection in ASP.NET MVC - DotNetFunda.com[^]

Keep in mind that when going to an anonymous or dynamic type, you will lose all the good stuff that a strong-type has. (e.g compile-time checking and intellisense for your View). So think about it. The Controller could end up passing any type to the View while at runtime. We can't even analyze the code and guess, because action filters could change the object passed to the View for all we know.

My recommendation is use strongly-typed View and return a concrete model which houses only the fields that you want in the View just like what is already suggested by F-ES Sitecore.


这篇关于我在LINQ查询中收到一条错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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