Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”_C#_Generics - Fatal编程技术网

C# 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”

C# 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”,c#,generics,C#,Generics,我已经看过stackoverflow中提供的所有解决方案,但没有得到解决方案。下面是我的代码 private static List<CombinedData> CombineReport(List<TimesheetUserPaycodeReport> timesheetUserPayCodeData, List<TimesheetReportData> reportData) { List<CombinedDa

我已经看过stackoverflow中提供的所有解决方案,但没有得到解决方案。下面是我的代码

private static List<CombinedData> CombineReport(List<TimesheetUserPaycodeReport> timesheetUserPayCodeData, List<TimesheetReportData> reportData)
        {
            List<CombinedData> combinedReportData = new List<CombinedData>();
            return combinedReportData = (from ts in timesheetUserPayCodeData
            join rd in  reportData on new {ts.LoginName,ts.EntryDateString } equals new {rd.LoginName,rd.EntryDateString }
            select new { rd.EmployeeId,rd.LoginName, rd.LastName, rd.FirstName,rd.StartDateString,
                rd.EndDateString,rd.UserId,rd.EntryDateString,rd.ApprovalStatus,ts.PayCodeHours,
                ts.PayCodeCode,rd.ProjectCode,rd.OTRTicket,rd.ActivityCode,rd.ExportOn,rd.ApprovedOnString,rd.SubmittedOnString,rd.TimesheetId}).ToList();   
我得到以下错误:

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<TimeSheetExport.CombinedData>

您正在尝试创建一个异常类型的列表,而不是列表。 试一试

您试图从一个方法返回匿名类型的列表,该方法 需要返回列表。新的{}将创建 匿名类型列表,因此您需要将列表创建为 方法的返回类型

我猜,您的CombinedData类包含您要返回的所有字段,然后只需按以下方式更改查询:

private static List<CombinedData> CombineReport(List<TimesheetUserPaycodeReport> timesheetUserPayCodeData, List<TimesheetReportData> reportData)
        {
            List<CombinedData> combinedReportData = new List<CombinedData>();
            return combinedReportData = (from ts in timesheetUserPayCodeData
            join rd in  reportData on new {ts.LoginName,ts.EntryDateString } equals new {rd.LoginName,rd.EntryDateString }
            select new CombinedData   /// Create CombinedData object here, not anonymous type
            {  
              EmployeeId = rd.EmployeeId,
              LoginName = rd.LoginName, 
              LastName = rd.LastName, 
             ------ so on for remaining fields
             }).ToList();

错误很明显。您试图从需要返回Listnamespace TimeSheetExport{[DelimitedRecord,]class CombinedData{[FieldQuoted]公共字符串EmployeeId;[FieldQuoted]的方法返回匿名类型的列表public string LoginName;请不要尝试在注释中添加代码-如果要显示更多代码,请编辑问题并添加它。如果突出显示代码块并点击{}ToListSide注意:您应该提供指向您尝试过的方法的链接,并简要说明为什么它在您的案例中不起作用,而不是搜索大量文本。
private static List<CombinedData> CombineReport(List<TimesheetUserPaycodeReport> timesheetUserPayCodeData, List<TimesheetReportData> reportData)
        {
            List<CombinedData> combinedReportData = new List<CombinedData>();
            return combinedReportData = (from ts in timesheetUserPayCodeData
            join rd in  reportData on new {ts.LoginName,ts.EntryDateString } equals new {rd.LoginName,rd.EntryDateString }
            select new CombinedData   /// Create CombinedData object here, not anonymous type
            {  
              EmployeeId = rd.EmployeeId,
              LoginName = rd.LoginName, 
              LastName = rd.LastName, 
             ------ so on for remaining fields
             }).ToList();