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

5

我正在尝试将列表带到前端。

我使用的是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 到 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();
}

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

2个回答

5
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()

2
将您的代码更改为以下内容可能会起作用: "将您的代码更改为这个也许可以工作:"
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();
        }

2
query 变量末尾的 .ToList() 是多余的。 - Tubs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接