我有四门课:
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Product> Product { get; set; }
}
public class Product
{
public int ProductNumber { get; set; }
public string ProductColor { get; set; }
}
///////////////////////////////////////////////
public class Customer_
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Article> Article { get; set; }
}
public class Article
{
public int ArticleNumber { get; set; }
public string ArticleColor { get; set; }
}
还有一个例子:
var Cus = new List<Customer>
{
new Customer()
{
FirstName = "FirstName1",
LastName = "LastName1",
Product = new List<Product>
{
new Product()
{
ProductColor = "ProductColor1",
ProductNumber = 11
}
}
},
new Customer()
{
FirstName = "FirstName2",
LastName = "LastName2",
Product = new List<Product>
{
new Product()
{
ProductColor = "ProductColor2",
ProductNumber = 12
}
}
}
};
我想创建一个新对象List<Customer_>
与我的实例的价值Cus
。例如Customer.FirstName = Customer_.FirstName, Customer.Product.ProductColor = Customer_.Article.ArticleColor etc
轻松做到这一点的最佳方法是什么,可以使用Dictionary
?
映射可以通过使用Interface https://msdn.microsoft.com/en-us/library/87d83y5b.aspx.
定义一个接口,它提供逻辑命名属性的映射,例如您提到的常见颜色属性:
// Some entities have different named properties but can be joined
// using those properties. This interface shows a common color which
// when implemented will route the processing to a common shared property
// which reports and sets the associated color.
public interface IDefinedColor
{
string Color { get; set; }
}
如果你必须创建partial
课程为Product
and Article
并让它们遵守所述接口。提示如果使用实体映射器(例如 EF),这是使用部分进行此类映射的好方法。实现接口并连接通用性:
// Holds the common properties for future processing.
public partial class Product : IDefinedColor
{
public string Color
{
get { return ProductColor; }
set { ProductColor = value; }
}
}
然后工作IDefinedColor
mapped根据需要实施。
通过使用接口,可以让所有未来的开发人员知道契约,该契约指定属性中的业务逻辑相等性,并且它不会隐藏在其他属性中joining类。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)