none
Projection on Included Child Table RRS feed

  • Question

  • I have a table named SerialNumberConfigurations that has a many table named SerialSequenceRanges. I want to get just the PartNbr field from the parent table, and just the DashNumber field from the child. The code below works in getting just the PartNbr, but I don't know how to get just the DashNumber from the SerialSequenceRanges table. How do you do a projection on the child table?

    var configs = context.SerialNumberConfigurations.Select(c => 
         new { PartNbr = c.PartNumber, Seqs = c.SerialSequenceRanges }).ToList();

                   

    Monday, February 29, 2016 11:20 PM

Answers

  • This works, but I couldn't get it to work with Include.

    var configs = context.SerialNumberConfigurations
    	.Where(c => c.SerialType == "Unit")
    	.Select(c => new { PartNbr = c.PartNumber, c.DashPattern, 
    		Seqs = from s in c.SerialSequenceRanges 
    			select new { s.DashNumbers } }).ToList();

    Thursday, March 3, 2016 1:15 AM
  • Hi FireStorm2012,

    >>How do you do a projection on the child table?

    If two table is one to many relationships. You could refer the code like below to implement it. and property named “b” is a collection.

    var result = (from s in db.Suppliers
                  select new
                  {
                      a = s.Name,
                      b = s.Products.Select(c=> c.Name)
                  }).ToList();
    

    If two table is one to one relationships. You could refer the code like below to implement it.

    var result2 = from p in db.Products
                                  select new
                                  {
                                      a = p.Name,
                                      b = p.Supplier.Name
                                  }
    

    Best regards,

    Cole Wu

    Thursday, March 3, 2016 9:53 AM
    Moderator

All replies

  • The Include statement..

    https://msdn.microsoft.com/en-us/data/jj574232.aspx?f=255&MSPPError=-2147217396

    Tuesday, March 1, 2016 12:37 AM
  • This works, but I couldn't get it to work with Include.

    var configs = context.SerialNumberConfigurations
    	.Where(c => c.SerialType == "Unit")
    	.Select(c => new { PartNbr = c.PartNumber, c.DashPattern, 
    		Seqs = from s in c.SerialSequenceRanges 
    			select new { s.DashNumbers } }).ToList();

    Thursday, March 3, 2016 1:15 AM
  • Hi FireStorm2012,

    >>How do you do a projection on the child table?

    If two table is one to many relationships. You could refer the code like below to implement it. and property named “b” is a collection.

    var result = (from s in db.Suppliers
                  select new
                  {
                      a = s.Name,
                      b = s.Products.Select(c=> c.Name)
                  }).ToList();
    

    If two table is one to one relationships. You could refer the code like below to implement it.

    var result2 = from p in db.Products
                                  select new
                                  {
                                      a = p.Name,
                                      b = p.Supplier.Name
                                  }
    

    Best regards,

    Cole Wu

    Thursday, March 3, 2016 9:53 AM
    Moderator