简体   繁体   English

在ASP.NET MVC中使用IQueryable仅选择特定的列

[英]Selecting only specific columns using IQueryable in asp.net mvc

I'm kinda new to asp.net mvc. 我是asp.net mvc的新手。 I'm currently working with code that has been developed by another person and have come to a dead end. 我目前正在使用由另一个人开发的代码,但最终陷入僵局。

IQueryable<DatatableTrainingHistory> trainingView = (from b in db.DatatableTrainingHistorys
                                                     select b);

/* Returns the format needed to output DataTable extension */
return DataTablesResult.Create(trainingView, dataTableParam, uv => new
{
      //stuff runs here
});

What this code does is it selects data from all columns in a table. 该代码的作用是从表的所有列中选择数据。 What I currently need is for the code to select specific columns, let's say TrainingName , TrainingTime , Location , and Quota . 我目前需要的是代码选择特定的列,例如TrainingNameTrainingTimeLocationQuota The output must be an IQueryable type since the code uses this extension to output the data. 输出必须是IQueryable类型,因为代码使用扩展名来输出数据。

I have tried numerous ways to try to solve this. 我尝试了多种方法来解决此问题。

(1) (1)

IQueryable<DatatableTrainingHistory> trainingView = (from b in db.DatatableTrainingHistorys
                                                    select new DatatableTrainingHistory
                                                    {
                                                        TrainingName = b.TrainingName,
                                                        TrainingTime = b.TrainingTime,
                                                        Quota = b.Quota,
                                                        Location = b.Location,
                                                    });

The code above outputs an error: 上面的代码输出错误:

The entity or complex type 'AppModel.DatatableTrainingHistory' cannot be constructed in a LINQ to Entities query.

(2) (2)

IQueryable<DatatableTrainingHistory> trainingView = (from b in db.DatatableTrainingHistorys
                                                    select new
                                                    {
                                                        TrainingName = b.TrainingName,
                                                        TrainingTime = b.TrainingTime,
                                                        Quota = b.Quota,
                                                        Location = b.Location,
                                                    }).AsQueryable();

The code above shows an error saying: 上面的代码显示了一条错误消息:

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<Project.Models.DatatableTrainingHistory>'. An explicit conversion exists (are you missing a cast?)

Can anyone kindly point me to the right direction? 谁能指出我正确的方向? Thank you. 谢谢。

(1). (1)。 DatatableTrainingHistory is an entity object and you simply cannot use entity object in LINQ query. DatatableTrainingHistory是一个实体对象,您根本无法在LINQ查询中使用实体对象。

(2). (2)。 You're returning anonymous object , so you should use var for it 您要返回anonymous object ,因此应使用var

var trainingView = (from b in db.DatatableTrainingHistorys
                                                    select new
                                                    {
                                                        TrainingName = b.TrainingName,
                                                        TrainingTime = b.TrainingTime,
                                                        Quota = b.Quota,
                                                        Location = b.Location,
                                                    }).AsQueryable();

There're two possible solutions for this: 有两种可能的解决方案:

  • use anonymous object (as point (2) in your question) 使用匿名对象(问题中的第(2)点)
  • introduce new data transfer object 引入新的数据传输对象

both of them has pros and cons , the second solution requires more efforts, but it will give you strongly type => less errors. 他们两个都有优点缺点 ,第二个解决方案需要付出更多的努力,但是它将为您提供强类型=>更少的错误。 There're couple of libraries to do the object mapping automatically for you (for example: AutoMapper ), maybe you can try them out. 有几个库可以为您自动进行对象映射(例如: AutoMapper ),也许您可​​以尝试一下。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM