我有一个LINQ到SQL对象,有一些其他表的引用如何MapFrom集合?自动映射器
我想映射它到一个虚拟机,但没有任何获取捕获。
Mapper.CreateMap<A, G>();
// A is the linq to sql object
A.MyList // this is a collection that I am trying to get the value out of A.MyList.Id
// G is my View Model
public class G
{
public string AMyListId {get; set;}
}
vm = Mapper.Map<List<A>, List<G>>(aListOfAFromDb);
这总是从null返回。我以为我必须手动做,所以我试过
Mapper.CreateMap<A, G>().ForMember(dest => dest.AMyList , opt => opt.MapFrom(src =>????))
;
但由于我从列表中得到它,它不会给任何属性选择。
编辑
我意识到,我不应该有“MYLIST”名单应该是一对一的。我仍然有问题想要做我想做的事情。
我有这个
Mapper.CreateMap();
A.Vip.UserId // again my linq object is A
// G is my View Model
public class G
{
public string IsVip {get; set;}
}
vm = Mapper.Map<List<A>, List<G>>(aListOfAFromDb);
Mapper.CreateMap<A, G>().ForMember(dest => dest.IsVip, opt => opt.AddFormatter<VipFormatter>());
public class VipFormatter : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
bool value = (bool)context.SourceValue;
if (value)
{
return "Yes";
}
return "No";
}
}
但没有任何东西每一个都受到约束。我不知道为什么。我必须将我的财产更改为“AVipUserId”吗?或者以某种方式告诉它映射?
某些东西对我来说没有意义...如果A包含一个列表,并且G包含列表中某个项目的ID,那么您应该从A映射到列表,否? (我的意思是,你有许多项目对一个“A”和一个项目对一个“G”)也许这就是为什么你很难映射的东西。 –
Remus
2011-02-23 22:03:40
如果你在“G”有一个属性“IsVip”,也许你应该使它成为一个bool类型。另外,你可以提一下你在这里使用的逻辑吗?如何认识到“A”是否是“VIP”?它是基于A.Vip对象是否为空? – Remus 2011-02-23 22:41:56