如何将IEnumerable <t>映射到字典<int,t>

时间:2019-01-28 06:41:18

标签: c#

我有一个具有这种返回类型的方法:

IEnumerable<T>

我想映射此方法并填写字典 预期结果是: Dictionary<int, T>

Dictionary<int, LeaveTypeDto> leaveType = new Dictionary<int, LeaveTypeDto>();
LeaveType.GetList(string.Empty).ToDictionary<int, LeaveTypeDto>();

我该怎么做? 注意:GetList retrun Type是IEnumerable的LeaveType,某些字段必须映射在LeaveTypeDto中,而某些字段则映射在LeaveType中:

"ID,Title,HourlyAvailable,..."

ID是唯一的,必须在我的字典中用作“键”,而其他字段必须映射到“ LeaveTypeDto”。

4 个答案:

答案 0 :(得分:0)

您可以指定要从枚举中获取的键,或者键和值。

如果我有这样的课程:

public class MyClass
{
    public int Id {get;set;}
    public string Name {get;set;}
}

我可以编写以下内容以获得Dictionary<int, MyClass>

IEnumerable<MyClass> values;
IDictionary<int, MyClass> valueDict = values.ToDictionary(t => t.Id);

或以下内容获得Dictionary<int, string>

IEnumerable<MyClass> values;
IDictionary<int, string> valueDict = values.ToDictionary(t => t.Id, t => t.Name);

答案 1 :(得分:0)

System.Linq

GetList(string.Empty).ToDictionary(k => int.Parse(k.ID))

请注意,在前面的例子是有效的,如果IDstring,如果是int,则甚至更简单的

GetList(string.Empty).ToDictionary(k => k.ID)

答案 2 :(得分:0)

如果ID是LeaveTypeDto的一部分

var q = GetList(string.Empty).ToDictionary(b => b.ID);

否则使用select重载获取索引,然后使用ToDictionary()

var q = GetList(string.Empty).Select((element, index) => new { i= index, e = element }).ToDictionary(b => b.i, b => b.e);

答案 3 :(得分:0)

我在LeaveType上定义了一种扩展方法:

@echo off
setlocal
::Define LF variable containing a linefeed (0x0A)
set LF=^


::NOTE! the above 2 blank lines are critical - do not remove

::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

setlocal enableDelayedExpansion
::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
findstr /n /r /c:"\[Terminal\]!CR!*!LF!Fenetre=O!CR!*!LF!Debug=O" C:\ETMI\ECSPyx\pyxvital\Pyxvital.ini>nul && echo Found || echo Not found

并且通过这一行我的问题已解决。:)

public static DTO.LeaveTypeDto ToDto(this DataModel.Timekeeper.LeaveType leaveType)
        {
            return new DTO.LeaveTypeDto
            {
                ID = leaveType.ID,
                Code = leaveType.Code,
                Title = leaveType.Title,
                DailyAvailable = leaveType.DailyAvailable,
                HourlyAvailable = leaveType.HourlyAvailable,
                ShiftBaseAvailable = leaveType.ShiftBaseAvailable,
                PredefinedRemaining = leaveType.PredefinedRemaining,
                Active = leaveType.Active,
                StandardLeaveType = leaveType.StandardLeaveType,
                NotRequestAutomaticaly = leaveType.NotRequestAutomaticaly,
                TemplateFileID = leaveType.TemplateFileID,
                ObligateChooseSubstituteForDailyLeave = leaveType.ObligateChooseSubstituteForDailyLeave,
                ObligateChooseSubstituteForHourlyLeave = leaveType.ObligateChooseSubstituteForHourlyLeave,
                ObligateChooseSubstituteForShiftBaseLeave = leaveType.ObligateChooseSubstituteForShiftBaseLeave,
                ObligateChooseInsertAttachment = leaveType.ObligateChooseInsertAttachment,
                CheckSubstituteLimit = leaveType.CheckSubstituteLimit,
                AbsenceRespiteYearXferLimitDays = leaveType.AbsenceRespiteYearXferLimitDays,
                AbsenceRespiteYearXferMode = leaveType.AbsenceRespiteYearXferMode,
                AbsenceRespiteYearXferValuesSummarize = leaveType.AbsenceRespiteYearXferValuesSummarize,
                DailyRequestRegisterRespite = leaveType.DailyRequestRegisterRespite,
                DayMaxHourlyMinutes = leaveType.DayMaxHourlyMinutes,
                DayWorkMinutes = leaveType.WorkingPeriodMaxHourlyMinutes,
                DecreaseAllOnNXOverflow = leaveType.DecreaseAllOnNXOverflow,
                DecreaseBasedOnMonthWorkingDays = leaveType.DecreaseBasedOnMonthWorkingDays,
                DecreaseDayWorkMinutesOnExtraWorkDays = leaveType.DecreaseDayWorkMinutesOnExtraWorkDays,
                DecreaseOnExtraWorkDays = leaveType.DecreaseOnExtraWorkDays,
                DecreaseOnHolidays = leaveType.DecreaseOnHolidays,
                HourlyInOFFDayAvailable=leaveType.HourlyInOFFDayAvailable,
                YearXferValuesSummarize = leaveType.YearXferValuesSummarize,
                YearXferPercentage=leaveType.YearXferPercentage,
                MaxContinuousDaysLimit = leaveType.MaxContinuousDaysLimit,
                HourlyRequestRegisterRespite = leaveType.HourlyRequestRegisterRespite,
                YearXferMode = leaveType.YearXferMode,
                MonthlyDaysLimit = leaveType.MonthlyDaysLimit,
                MonthlyXMinsLimit = leaveType.MonthlyXMinsLimit,
                YearXferLimitDays = leaveType.YearXferLimitDays,
                YearLimitDays = leaveType.YearLimitDays,
                MonthStorable = leaveType.MonthStorable,
                YearHourlyMinsLimit = leaveType.YearHourlyMinsLimit,
                MonthPreusable = leaveType.MonthPreusable,
                MonthlyNXMinsLimit = leaveType.MonthlyNXMinsLimit,
                MonthMaxHourlyMinutes = leaveType.MonthMaxHourlyMinutes,
                IncreaseAbsenceRespiteRemaining = leaveType.IncreaseAbsenceRespiteRemaining,
                DailyLeaveRequestCountPerMonthForOthers = leaveType.DailyLeaveRequestCountPerMonthForOthers,
                DailyLeaveRequestDurationPerMonthForOthers = leaveType.DailyLeaveRequestDurationPerMonthForOthers,
                ShiftLeaveRequestCountPerMonthForOthers = leaveType.ShiftLeaveRequestCountPerMonthForOthers,
                ShiftLeaveRequestDurationPerMonthForOthers = leaveType.ShiftLeaveRequestDurationPerMonthForOthers,
                HourlyLeaveRequestCountPerMonthForOthers = leaveType.HourlyLeaveRequestCountPerMonthForOthers,
                UseLeaveRequetLimitForOthersPerMonth = leaveType.UseLeaveRequetLimitForOthersPerMonth,
                DailyCountPerMonth = leaveType.DailyCountPerMonth,
                CheckMeritRemainingInUnpayedRequest = leaveType.CheckMeritRemainingInUnpayedRequest,
                DayMinHourlyMinutes = leaveType.DayMinHourlyMinutes,
                FirstPresenceRangeHourlyCountPerMonth = leaveType.FirstPresenceRangeHourlyCountPerMonth,
                FirstPresenceRangeLenght = leaveType.FirstPresenceRangeLenght,
                FirstPresenceRangeMaxHourlyLimit = leaveType.FirstPresenceRangeMaxHourlyLimit,
                FirstPresenceRangeMinHourlyLimit= leaveType.FirstPresenceRangeMinHourlyLimit,
                HourlyCountPerMonth = leaveType.HourlyCountPerMonth,
                HourlyLeaveRequestDurationPerMonthForOthers = leaveType.HourlyLeaveRequestDurationPerMonthForOthers,
                IsMonthMaxHourlyStorableToCurrent = leaveType.IsMonthMaxHourlyStorableToCurrent,
                LastPresenceRangeHourlyCountPerMonth = leaveType.LastPresenceRangeHourlyCountPerMonth,
                LastPresenceRangeLenght = leaveType.LastPresenceRangeLenght,
                LastPresenceRangeMaxHourlyLimit = leaveType.LastPresenceRangeMaxHourlyLimit,
                LastPresenceRangeMinHourlyLimit = leaveType.LastPresenceRangeMinHourlyLimit,
                MiddlePresenceRangeHourlyCountPerMonth = leaveType.MiddlePresenceRangeHourlyCountPerMonth,
                MiddlePresenceRangeMaxHourlyLimit = leaveType.MiddlePresenceRangeMaxHourlyLimit,
                MiddlePresenceRangeMinHourlyLimit = leaveType.MiddlePresenceRangeMinHourlyLimit,
                MonthMaxDailyDays=leaveType.MonthMaxDailyDays,
                MonthMaxHourlyStorable=leaveType.MonthMaxHourlyStorable,
                ShiftBaseCountPerMonth=leaveType.ShiftBaseCountPerMonth,
                SuspendsPersonnelStatus=leaveType.SuspendsPersonnelStatus,
                WorkingPeriodMaxHourlyMinutes=leaveType.WorkingPeriodMaxHourlyMinutes,
                YearXMonthMaxHourlyferLimitHours=leaveType.YearXMonthMaxHourlyferLimitHours,
                YearXMonthMaxHourlyferMode=leaveType.YearXMonthMaxHourlyferMode
            };
        }