0

I have 3 tables with 1:many parent child relation

  • TableA
  • TableB (child of TableA)
  • TableC (child of TableB)

I am able to retrieve all of the data from these tables with query

var data = dbContext.tableA.where(a => a.ID == rowID)
           .Include(a => a.tableB.Select(n => n.tableC)).SingleOrDefault();

For TableC, I don't want all of the columns to be retrieved from database. I just want ID, TableC_FK columns data to be retrieved for TableC.

How do I do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ravi Teja
  • 1,097
  • 8
  • 13

1 Answers1

0

I think you cannot project into TableA unless you have scheme that support it. IMHO The best option is either anonymous project or Use Viewmodel/DTO suggested by @jpgrassi

You can use anonymous here, more tutorial of anonymous projection here

var data = dbContext.tableA
                    .where(a => a.ID == rowID)
                    .Select(tableA=> new 
                    {
                       firstColumn = tableA.FirstColumn,
                       tableC = tableA.SelectMany(tableB=>tableB.TableC),
                    }.SingleOrDefault();
Eldho
  • 7,795
  • 5
  • 40
  • 77