简体   繁体   中英

Cannot convert type IEnumerable to List C#

I'm trying to bring a listing to frontEnd . I'm using mongoDb. My mongodb has a colletion called Employee . Employee has the following attribute

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; }
}

In this Model , I have an attribute called: public List <DocumentsImagesViewModel> DependentsDocuments {get; set; } public List <DocumentsImagesViewModel> DependentsDocuments {get; set; } 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; }
}

I'm trying to bring benefits that contain the depedentID equal of the parameter. When I use this method, it has an error that can not be converted. IEnumerable to List C #

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)                            
                };               
    return query.ToList();
}

What is the best way to get this data? this filter? I used this question as a reference: Mongodb C# driver return only matching sub documents in array

LINQ's .Where returns IEnumerable<T> , your model expects a List<T> , you can either change your model to IEnumberale<T> or you can change this line of code:

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

To This:

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

changing your code to this one maybe it work:

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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM