我正在尝试将列表列入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#
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# driver return only matching sub documents in array
答案 0 :(得分:5)
LINQ的.Where
返回IEnumerable<T>
,您的模型需要一个List<T>
,您可以将模型更改为IEnumberale<T>
,也可以更改以下代码行:
DependentsDocuments = employee.DependentsDocuments.Where(x =>
x.dependentId == dependentId)
为此:
DependentsDocuments = employee.DependentsDocuments.Where(x =>
x.dependentId == dependentId).ToList()
答案 1 :(得分:4)
将您的代码更改为此代码可能可行:
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();
}