我有以下类添加映射器
public class OrderMapper<T> {
public OrderMapper<T> Add<TKey>(Expression<Func<T, TKey>> expression, String name) {
}
}
我使用如下:
OrderMapper<Post> mapper = new OrderMapper<Post>().Add(x => x.Created, "created");
问题是有时候我有以下内容:
posts.SelectMany(x => x.Category, (Post, Category) => new { Post, Category });
返回匿名类型列表:
var objects = new { Post post, Category category }
我不确定如何创建此类型的OrderMapper ......
我想也许我需要将我的类方法更改为扩展名:
OrderMapper mapper = objects.Add(...).Add(...)
有人能告诉我最好的方法吗?
答案 0 :(得分:3)
您可以对Create
类使用OrderMapper
方法:
public class OrderMapper
{
public static OrderMapper<T> CreateMapperFor<T>(IEnumerable<T> typeProvider)
{
return new OrderMapper<T>();
}
}
并且,使用它:
var objects = Enumerable.Repeat(new { /* ... */ }, 1);
var mapper = OrderMapper.CreateMapperFor(objects)
.Add(/*...*/)
.Add(/*...*/);