Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert type IEnumerable to List C#

Tags:

c#

mongodb

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

like image 494
Eliemerson Fonseca Avatar asked Apr 22 '19 12:04

Eliemerson Fonseca


People also ask

Can I convert IEnumerable to list?

In C#, an IEnumerable can be converted to a List through the following lines of code: IEnumerable enumerable = Enumerable. Range(1, 300); List asList = enumerable. ToList();

What is IEnumerable C#?

IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.


2 Answers

LINQ's .Where returns IEnumerable<T>, your model expects a List<T>, you can either change your model to IEnumerable<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()
like image 153
Ryan Wilson Avatar answered Oct 02 '22 21:10

Ryan Wilson


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();
        }
like image 21
hassan.ef Avatar answered Oct 02 '22 22:10

hassan.ef