[英]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. 我有一个get控制器,可以检索所有品牌,以前我使用的是DTO,但发现了匿名对象,因此我决定尝试一下。
My get method using DTO's was the following: 我使用DTO的get方法如下:
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#. 真的,我的问题是C#编译器如何知道如何映射内部对象并且不重复属性,例如,在使用DTO的I时,在Brands类中,我有一个brandID和一个Product对象,在Product类中也有一个brandId。必须手动指定品牌DTO中的brandId并在ProductDTO中删除该属性,以便不重复数据,但是当我在内部使用匿名对象c#时,c#也会自动完成内部对象(产品)。 I'm really surprised that c# could do this without the need to manually specify it
我真的很惊讶C#无需手动指定就可以做到这一点
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 并仅指定我想要的属性
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.
编译器仅创建一个具有五个属性的隐藏类,这些属性的名称和类型与您从Brand对象中选择的名称和类型相同,然后EF实例化此类对象,而不是dtos。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.