Angel Silva
Angel Silva

Reputation: 375

How to anonymous object works c# entity framework

I have a get controller that retrieves all the brands, previously I was using DTO's but found out anonymous objects so I decided to give it a try.

My get method using DTO's was the following:

 public async Task < IHttpActionResult > GetBrand(int id) {
     var brand = await db.Brand.Select(x => new {
       brandDesc = x.brandDesc,
       BrandId = x.BrandId,
       brandLogoUrl = x.brandLogoUrl,
       brandName = x.brandName,
       Products = x.Products.Select(y => new {
            productDesc = y.productDesc,
            ProductId = y.ProductId,
            productName = y.productName,
            productPrice = y.productPrice,
            productStock = y.productStock,
            productStatus = y.productStatus,
            productModifyDate = y.productModifyDate
       }).ToList()
     }).FirstOrDefaultAsync(x => x.BrandId == id);
        if (brand == null) {
            return NotFound();
        }
        return Ok(brand);
    }

My new code with anonymous objects looks like this:

public async Task < IHttpActionResult > GetBrand(int id) {
    var brand = await db.Brand.Select(x => new {
        x.brandDesc,
        x.BrandId,
        x.brandLogoUrl,
        x.brandName,
        x.Products
    }).FirstOrDefaultAsync(x => x.BrandId == id);
    if (brand == null) {
        return NotFound();
    }
    return Ok(brand);
}

They both return the same output:

[{
"brandDesc": "Dicalc phos crys-forearm",
"BrandId": 7,
"brandLogoUrl": "http://dummyimage.com/159x219.png/5fa2dd/ffffff",
"brandName": "ALK-Abello, Inc.",
"Products": [
  {
    "productDesc": "Unspecified umbilical cord complication complicating labor and delivery, antepartum condition or complication",
    "ProductId": 70,
    "productName": "Bigtax",
    "productPrice": 4445.17,
    "productStock": 39,
    "productStatus": true,
    "productModifyDate": "2016-06-03T08:26:24"
  },
  {
    "productDesc": "Adhesions of iris, unspecified",
    "ProductId": 598,
    "productName": "It",
    "productPrice": 1240.36,
    "productStock": 35,
    "productStatus": false,
    "productModifyDate": "2016-06-04T01:00:54"
  }
]

}]

Really my question here is how does C# compiler know how to map the internal object and do not repeat properties, for example in the Brands Class I have a brandID and a Product object, the Product class also has a brandId, when I use DTO's I have to specify manually map the brandId in the Brand DTO and delete that property in the ProductDTO so data is not repeated, but when I use anonymous objects c# internally does this, also the internal object(product) is done in automatic by c#. I'm really surprised that c# could do this without the need to manually specify it

Edit: also found out that I can manually specify a property within the anonymous object:

Products = x.Products.Select(y => new {
   productDesc = y.productDesc,
   ProductId = y.ProductId,
   productName = y.productName,
   productPrice = y.productPrice,
   y.productStock,
   y.productStatus,
   y.productModifyDate}).ToList()

And specify only the properties that I want

Upvotes: 0

Views: 341

Answers (1)

Jonas H&#248;gh
Jonas H&#248;gh

Reputation: 10884

There is no magic here. The compiler simply creates a hidden class with five properties with identical names and types to the ones you select from the Brand object, and EF instantiates such objects instead of dtos.

Upvotes: 1

Related Questions