Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表自动映射到按日期分组的集合_C#_Automapper - Fatal编程技术网

C# 将列表自动映射到按日期分组的集合

C# 将列表自动映射到按日期分组的集合,c#,automapper,C#,Automapper,我有一个父子源,我希望使用AutoMapper将其映射到父日期子目的地(例如,目的地使用SortedSet集合按日期对子数据进行分组) 这样做的目的是,您可以使用SortedSet获取该日期的所有“子项” 我目前使用的是一个循环,循环源和单独映射的子项,然后将它们添加到SortedSet。从下面的代码可以看出,如果日期不存在,则需要在SortedSet集合中创建一个新项 有没有更好的方法来配置映射 我知道AutoMapper可以映射列表(而不仅仅是单个列表项),但由于需要按日期对目标项进行分组,

我有一个父子源,我希望使用AutoMapper将其映射到父日期子目的地(例如,目的地使用SortedSet集合按日期对子数据进行分组)

这样做的目的是,您可以使用SortedSet获取该日期的所有“子项”

我目前使用的是一个循环,循环源和单独映射的子项,然后将它们添加到SortedSet。从下面的代码可以看出,如果日期不存在,则需要在SortedSet集合中创建一个新项

有没有更好的方法来配置映射

我知道AutoMapper可以映射列表(而不仅仅是单个列表项),但由于需要按日期对目标项进行分组,我不确定是否有更好的解决方案来解决我当前使用的问题

源类型
公共类视图模型
{
公共视图模型()
{
天数=新分拣集();
}
公共分类数据集天数{get;set;}
}
公共类模型儿童
{
公共日期时间已开始{get;set;}
公共字符串说明{get;set;}
}
公共类ModelParent
{
公共字符串名称;
public IList Children=新列表();
}
目的地类型
公共类ChildViewModel
{
公共日期时间已开始{get;set;}
公共字符串说明{get;set;}
}
公共类DayViewModel
{
公共日期时间日期{get;set;}
公共DayViewModel()
{
Children=新列表();
}
公共IList子项{get;set;}
}
公共类日比较程序:IComparer
{
公共整数比较(DayViewModel x、DayViewModel y)
{
返回x.Date.CompareTo(y.Date);
}
}
公共类视图模型
{
公共视图模型()
{
天数=新的分类数据集(new DayComparer());
}
公共分类数据集天数{get;set;}
}
伊瓦卢埃索尔弗
公共类DaysResolver:IValueResolver
{
公共解决结果解决(解决结果源)
{
var person=(ModelParent)source.Context.SourceValue;
var vm=(ViewModel)source.Context.DestinationValue;
foreach(var modelChild-in-person.Children)
{
var file=Mapper.Map(modelChild);
var day=GetDay(vm,file.Started);
day.Children.Add(文件);
}
返回source.Ignore();
}
私有静态DayViewModel GetDay(ViewModel模型,日期时间键)
{
var dayViewModel=model.Days.FirstOrDefault(d=>d.Date==key);
如果(dayViewModel==null)
{
dayViewModel=新的dayViewModel
{
日期=键
};
model.Days.Add(dayViewModel);
}
返回日视图模型;
}
}
映射
private static void Main(字符串[]args)
{
CreateMap();
Mapper.CreateMap()
.FormMember(d=>d.Days,x=>x.ResolveUsing());
assertConfigurationsValid();
}

我想到的一个选项是将DayViewModel查找/创建逻辑移动到一个ConstructUsing方法中,用于ModelChild和DayViewModel之间的映射。例如

Mapper.CreateMap<ModelChild, DayViewModel>()
    .ConstructUsing(context =>
    {
        var key = ((ModelChild) context.SourceValue).Started;
        var daysCollection = (SortedSet<DayViewModel>) context.Parent.DestinationValue;

        var dayViewModel = daysCollection.FirstOrDefault(d => d.Date == key);

        if (dayViewModel == null)
        {
            dayViewModel = new DayViewModel
            {
                Date = key
            };

            daysCollection.Add(dayViewModel);
        }

        dayViewModel.Children.Add(Mapper.Map<ChildViewModel>(context.SourceValue));
        return dayViewModel;
    })
    .ForAllMembers(d => d.Ignore());
Mapper.CreateMap()
.ConstructUsing(上下文=>
{
var key=((ModelChild)context.SourceValue).Started;
var daysCollection=(SortedSet)context.Parent.DestinationValue;
var dayViewModel=daysCollection.FirstOrDefault(d=>d.Date==key);
如果(dayViewModel==null)
{
dayViewModel=新的dayViewModel
{
日期=键
};
daysCollection.Add(dayViewModel);
}
dayViewModel.Children.Add(Mapper.Map(context.SourceValue));
返回日视图模型;
})
.ForAllMembers(d=>d.Ignore());

这仍然会显式地再次调用Map,但我愿意接受其他建议

作为一个automapper noobie,很高兴知道
ConstructUsing()
带来了极大的灵活性并允许解决此问题
public class ChildViewModel 
{
    public DateTime Started { get; set; }
    public string Description { get; set; }
}

public class DayViewModel
{
    public DateTime Date { get; set; }

    public DayViewModel()
    {
        Children = new List<ChildViewModel>();
    }

    public IList<ChildViewModel> Children { get; set; }
}

public class DayComparer : IComparer<DayViewModel>
{
    public int Compare(DayViewModel x, DayViewModel y)
    {
        return x.Date.CompareTo(y.Date);
    }
}

public class ViewModel
{
    public ViewModel()
    {
        Days = new SortedSet<DayViewModel>(new DayComparer());
    }

    public SortedSet<DayViewModel> Days { get; set; }
}
public class DaysResolver : IValueResolver
{
    public ResolutionResult Resolve(ResolutionResult source)
    {
        var person = (ModelParent)source.Context.SourceValue;
        var vm = (ViewModel)source.Context.DestinationValue;

        foreach (var modelChild in person.Children)
        {
            var file = Mapper.Map<ChildViewModel>(modelChild);
            var day = GetDay(vm, file.Started);
            day.Children.Add(file);
        }
        return source.Ignore();
    }

    private static DayViewModel GetDay(ViewModel model, DateTime key)
    {
        var dayViewModel = model.Days.FirstOrDefault(d => d.Date == key);

        if (dayViewModel == null)
        {
            dayViewModel = new DayViewModel
            {
                Date = key
            };

            model.Days.Add(dayViewModel);
        }
        return dayViewModel;
    }
}
private static void Main(string[] args)
{
    Mapper.CreateMap<ModelChild, ChildViewModel>();

    Mapper.CreateMap<ModelParent, ViewModel>()
        .ForMember(d => d.Days, x => x.ResolveUsing<DaysResolver>());

    Mapper.AssertConfigurationIsValid();
}
Mapper.CreateMap<ModelChild, DayViewModel>()
    .ConstructUsing(context =>
    {
        var key = ((ModelChild) context.SourceValue).Started;
        var daysCollection = (SortedSet<DayViewModel>) context.Parent.DestinationValue;

        var dayViewModel = daysCollection.FirstOrDefault(d => d.Date == key);

        if (dayViewModel == null)
        {
            dayViewModel = new DayViewModel
            {
                Date = key
            };

            daysCollection.Add(dayViewModel);
        }

        dayViewModel.Children.Add(Mapper.Map<ChildViewModel>(context.SourceValue));
        return dayViewModel;
    })
    .ForAllMembers(d => d.Ignore());