无法将IEnumerable类型转换为List C#

Eli*_*eca 5 c# mongodb

我正在尝试将清单列出frontEnd。我正在使用mongoDb。我mongodb有一个名叫EmployeeEmployee具有以下属性

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; }
}
Run Code Online (Sandbox Code Playgroud)

在此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; }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试带来包含depedentID 相等参数的好处 。当我使用此方法时,它有一个无法转换的错误。IEnumerable列出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();
}
Run Code Online (Sandbox Code Playgroud)

获取此数据的最佳方法是什么?这个过滤器?我以这个问题作为参考:Mongodb C#驱动程序仅返回数组中匹配的子文档

Rya*_*son 5

LINQ的.Wherereturn IEnumerable<T>,您的模型期望a List<T>,您可以将模型更改为IEnumberale<T>,也可以更改以下代码行:

DependentsDocuments = employee.DependentsDocuments.Where(x =>
x.dependentId == dependentId)
Run Code Online (Sandbox Code Playgroud)

为此:

DependentsDocuments = employee.DependentsDocuments.Where(x =>
x.dependentId == dependentId).ToList()
Run Code Online (Sandbox Code Playgroud)