[英]c# MongoDB select list of fields from a field
Let's say i have this document structure stored in my MongoDB database. 假设我将此文档结构存储在MongoDB数据库中。
{
"_id" : ObjectId("54f131f3c4ca7b044eb11171"),
"services" : "gegsg",
"validCertificateOrLicense" : {
"title" : "asfasf",
"number" : "fasfsdf",
"dateIssued" : "fsdffsdfsdf",
"validUntil" : "fsdfsdfd",
"issuingAgency" : "fsdfsd"
}
}
I wanted to list those fields in my Binding to be used on my DataGrid, I have tried to use the solutions posted on several stack questions. 我想在我的Binding中列出要在我的DataGrid上使用的那些字段,我试图使用在几个堆栈问题上发布的解决方案。 But none of them worked, i tried to check using Console.WriteLine(BindList.Count) to check whether the query works.
但是没有一个工作,我试图检查使用Console.WriteLine(BindList.Count)来检查查询是否有效。
class facultyData
{
public ObjectId _id { get; set; }
public string services { get; set; }
public List<certification> validCertificateOrLicense { get; set; }
}
class certification
{
public string title { get; set; }
public string number { get; set; }
public string dateIssued { get; set; }
public string validUntil { get; set; }
public string issuingAgency { get; set; }
}
This is one of solution that i have tried. 这是我尝试过的解决方案之一。
var query = collection.AsQueryable<facultyData>()
.Where(c => c.validCertificateOrLicense.Any(d => d.stores.Count() == 1);
Or maybe there's a problem with my ToList code 或者我的ToList代码可能有问题
List<facultyData> acadList = query1.ToList<facultyData>();
BindingList<facultyData> acadBinding = new BindingList<facultyData>(acadList);
我认为这会奏效
var acadList = Database.GetCollection<facultyData>("facultyData").AsQueryable().Where(f => f.validCertificateOrLicense.Any()).ToList();
Your class facultyData
contains a list of certifications in the validCertificateOrLicense
field. 您的类
facultyData
包含validCertificateOrLicense
字段中的认证列表。 However your document structure in the DB only contains a single certificate in the validCertificateOrLicense
field. 但是,DB中的文档结构仅包含
validCertificateOrLicense
字段中的单个证书。
If your document structure in the DB is as follows then your query should work 如果数据库中的文档结构如下,则查询应该有效
{
"_id" : ObjectId("54f131f3c4ca7b044eb11171"),
"services" : "gegsg",
"validCertificateOrLicense" : [{
"title" : "asfasf",
"number" : "fasfsdf",
"dateIssued" : "fsdffsdfsdf",
"validUntil" : "fsdfsdfd",
"issuingAgency" : "fsdfsd"
}]
}
Note - the validCertificateOrLicense
field now contains an array of certification
sub documents. 注 -
validCertificateOrLicense
字段现在包含一系列certification
子文档。
Alternatively you could change your c# entity to match the existing document structure in the DB. 或者,您可以更改c#实体以匹配数据库中的现有文档结构。 Your facultyData object would look as follows
您的facultyData对象如下所示
class facultyData
{
public ObjectId _id { get; set; }
public string services { get; set; }
public certification validCertificateOrLicense { get; set; }
}
Note - now the validCertificateOrLicense
field only contains a single certification
document. 注 - 现在
validCertificateOrLicense
字段仅包含一个certification
文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.