简体   繁体   中英

Selecting only specific columns using IQueryable in asp.net mvc

I'm kinda new to 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 . The output must be an IQueryable type since the code uses this extension to output the data.

I have tried numerous ways to try to solve this.

(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)

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). DatatableTrainingHistory is an entity object and you simply cannot use entity object in LINQ query.

(2). You're returning anonymous object , so you should use var for it

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)
  • 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.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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