为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
红颜莎娜 2022-12-24 12:45:03
我正在尝试将列表带到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#
查看完整描述

2 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

LINQ 的.Where回报IEnumerable<T>,你的模型需要一个List<T>,你可以改变你的模型,IEnumerable<T>或者你可以改变这行代码:

DependentsDocuments = employee.DependentsDocuments
                              .Where(x => x.dependentId == dependentId)

对此:

DependentsDocuments = employee.DependentsDocuments
                              .Where(x => x.dependentId == dependentId)
                              .ToList()


查看完整回答
反对 回复 2022-12-24
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

将您的代码更改为此代码可能有效:


public async Task<List<Documents>> GetDocument(string ownerId, string dependentId)

        {

            var query = (from employee in _employee.AsQueryable()

                        where employee.ownerId == ownerId

                        select new Employee()

                        {

                           DependentsDocuments  = employee.DependentsDocuments.Where(x => x.dependentId == dependentId).ToList()                            

                        }).ToList();               

            return query.ToList();

        }


查看完整回答
反对 回复 2022-12-24
  • 2 回答
  • 0 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信