无法将类型 IEnumerable 转换为列表 C#

我正在尝试将列表带到frontEnd. 我正在使用 mongoDb。我mongodb有一个名为 的集合Employee。Employee具有以下属性


public class EmployeeViewModel

{

    [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]

    public string ownerId { get; set; }

    public string atributeChange { get;  set; }

    public PersonalDataViewModel personalData { get; set; }

    public AddressViewModel address { get; set; }

    public List<EmailsViewModel>  emails { get; set; }

    public SyndicateViewModel syndicate { get; set; }

    public List<DependentsViewModel> dependents { get; set; }

    public List<PhoneViewModel> phone { get; set; }

    public List<BankViewModel> bank { get; set; }

    public AttributesViewModel attributes { get; set; }

    public List<BenefitsViewModel> benefits { get; set; }

    public TransportViewModel transport { get; set; }

    public List<AttachmentsViewModel> attachments { get; set; }

    public List<DocumentsViewModel> documents { get; set; }

    public List<DocumentsImagesViewModel> DependentsDocuments { get; set; }

    public List<AttachmentsViewModel> DependentsAttachments { get; set; }

    public List<BenefitsViewModel> DependentsBenefits { get; set; }

}

在此Model,我有一个名为:的属性public List <DocumentsImagesViewModel> DependentsDocuments {get; set; }:


public class DocumentsViewModel

{

    [BsonId]

    public string ownerId { get; set; }

    public string id { get; set; }

    public string dependentId { get; set; }

    public string number { get; set; }

    public DateTime expiration { get; set; }

    public List<DocumentsImagesViewModel> images { get; set; }

    public List<DocumentPropertiesViewModel> properties { get; set; }

    public DocumentTypeViewModel type { get; set; }

}

我正在尝试带来包含 depedentID 参数相等的好处。当我使用这个方法时,它有一个无法转换的错误。IEnumerable 列出 C#


红颜莎娜
浏览 67回答 2
2回答

jeck猫

LINQ 的.Where回报IEnumerable<T>,你的模型需要一个List<T>,你可以改变你的模型,IEnumerable<T>或者你可以改变这行代码:DependentsDocuments&nbsp;=&nbsp;employee.DependentsDocuments &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(x&nbsp;=>&nbsp;x.dependentId&nbsp;==&nbsp;dependentId)对此:DependentsDocuments&nbsp;=&nbsp;employee.DependentsDocuments &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(x&nbsp;=>&nbsp;x.dependentId&nbsp;==&nbsp;dependentId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList()

幕布斯6054654

将您的代码更改为此代码可能有效:public async Task<List<Documents>> GetDocument(string ownerId, string dependentId)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var query = (from employee in _employee.AsQueryable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where employee.ownerId == ownerId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new Employee()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DependentsDocuments&nbsp; = employee.DependentsDocuments.Where(x => x.dependentId == dependentId).ToList()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return query.ToList();&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP