0

Using a Linq query, I have returned an array of two types and am trying assign each list of types to a list variable of the given type:

Model:

public class ReportViewModel
{
    public List<DbTable> DbTables;
    public List<Application> Applications;
}

Controller:

var ReportTablesAndSystems = (from tu in db.DbTablesUsed
                              join t in db.DbTables on tu.TableId equals t.TableId
                              join a in db.DbApplication on t.ApplicationId equals a.ApplicationId
                              where tu.ReportId == id
                              select new
                              {
                                  a,
                                  t
                              }).ToList();

ReportViewModel rvm = new ReportViewModel();

rvm.DbTables = //?????????
rvm.Applications = //??????

How can I set rvm.DbTables to be the list of DbTables which is held in ReportTablesAndSystems and set rvm.Applications to be the list of Applications held in ReportTablesAndSystems?

5
  • 2
    What does ReportTablesAndSystems contain? I.e. a List<T> -- what is T? Commented Nov 1, 2016 at 16:57
  • {Application a, DbTable t} Commented Nov 1, 2016 at 17:13
  • Your comment makes no sense. What type does the list contain? Commented Nov 1, 2016 at 17:28
  • 1
    ReportTablesAndSystems.Select(x =>x.a).ToList() ? Commented Nov 1, 2016 at 17:48
  • I think your ReportViewModel should only consist of one list of applications, each containing a collection of tables. Commented Nov 1, 2016 at 19:34

1 Answer 1

2

I have returned an array of two types

That would actually be a list of an anonymous type with properties a of type Application and t of type DbTable. ;)

You can assign your two properties like this then:

rvm.DbTables = ReportTablesAndSystems.Select(r => r.t).ToList();
rvm.Applications = ReportTablesAndSystems.Select(r => r.a).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - simple solution!
Alternatively : rvm.DbTables = (from r in ReportTablesAndSystems select r.t).ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.