无法将类型“System.Collections.Generic.List<<anonymous type: decimal SSN, string FullName>>”隐式转换为“System.Collections.Generic.List<>

问题描述 投票:0回答:1

当我尝试创建一种方法来返回所有没有用户的用户时,出现此错误 严重性代码 说明 项目文件行抑制状态 错误(活动)CS0029 无法将类型“System.Collections.Generic.List<>”隐式转换为“System.Collections.Generic.List”

我有两节课 头等舱

public partial class User
{
    public int Id { get; set; }

    public string? FullName { get; set; }

    public string? UserName { get; set; }

    public string? Password { get; set; }

    public string? Role { get; set; }

    public decimal? EmpId { get; set; }

    public DateTime? CreatedDate { get; set; }

    public DateTime? EditedDate { get; set; }

    public virtual Employee? Emp { get; set; }

}

第二班 公共部分类 Employee { 公共十进制 Ssn { 获取;放; }

public int? EmpCode { get; set; }

public string? EmpFirstName { get; set; }

public string? EmpSecondName { get; set; }

public string? EmpLastName { get; set; }

public virtual User? User { get; set; }

} 我想创建一个方法来返回所有没有用户的用户

我在EmployeesEF类中编写了这段代码

public List<string> GetAllEmpsIsNotUser()
{
    try
    {

        var query = from emp in db.Employees
               join user in db.Users on emp.Ssn equals user.EmpId into userGroup
               from u in userGroup.DefaultIfEmpty()
               where !db.Users.Any(u => u.EmpId == emp.Ssn)
               select new
               {
                   SSN = emp.Ssn,
                   FullName = emp.EmpFirstName + " " + emp.EmpSecondName + " " + emp.EmpLastName
               };

        return query.ToList();
    }
    catch
    {
        return new List<string>();

    }
}
c# entity-framework linq
1个回答
0
投票

query.ToList()
返回
List<dynamic>
,但该方法的返回类型为
List<string>
。您需要更新
select
子句以选择单个字符串值。

但是,声明的返回类型似乎不正确,您实际上想要同时具有 SSN 和 FullName 的内容。

您将无法根据您选择的匿名类型返回列表,并且需要创建一个类/记录来包含这些值。

© www.soinside.com 2019 - 2024. All rights reserved.