I have a mongodb which looks like this
[
{
"client_id": "abc",
"product_id": "123",
"weight": {
"value": 100
"unit": "kg"
}
},
{
"client_id": "def",
"product_id": "456",
"weight": {
"value": 200
"unit": "kg"
}
}
]
I need to get summation of weight value for a certain client id and product id using mongodb c# client, how can I do that?
I tried this but it is always returning 0
var total_weight = await Collection.AsQueryable()
.Where(
x => x.client_id == "abc" &&
x => x.product_id== "123")
.SumAsync(x => x.weight.value);
Thanks
I think you are looking for this query so you can try this code:
var total_weight = await Collection.AsQueryable<YourModel>()
.Where(x => x.client_id == "abc" && x.product_id == "123")
.GroupBy(x => new { x.client_id, x.product_id })
.Select(x => x.Sum(y => y.weight.value))
.FirstOrDefaultAsync();
Related
The problem
This works as expected and produces the result with the totalpax field being correct. However, the pax field for each destination, should be the sum based on the destination Id.
Code
public async Task<IEnumerable<ReservationCalendarGroupVM>> GetForCalendarAsync(string fromDate, string toDate) {
return await context.Schedules
.AsNoTracking()
.Where(x => x.Date >= Convert.ToDateTime(fromDate) && x.Date <= Convert.ToDateTime(toDate))
.GroupBy(z => new { z.Date })
.Select(e => new ReservationCalendarGroupVM {
Date = e.Key.Date.ToString(),
Destinations = e.GroupBy(i => new { i.Destination.Id, i.Destination.Abbreviation, i.Destination.Description }).Select(p => new DestinationCalendarVM {
Id = p.Key.Id,
Abbreviation = p.Key.Abbreviation,
Description = p.Key.Description,
Pax = context.Reservations.Where(y => y.Date == e.Key.Date).Sum(h => h.TotalPersons)
}),
TotalPax = context.Reservations.Where(y => y.Date == e.Key.Date).Sum(h => h.TotalPersons).ToListAsync();
}
Result
"date": "2022-07-02",
"destinations": [
{
"id": 1,
"description": "PAXOS - ANTIPAXOS",
"abbreviation": "PA",
"pax": 254
},
{
"id": 3,
"description": "BLUE LAGOON",
"abbreviation": "BL",
"pax": 254
}
],
"totalpax": 432
I believe the problem lies in this line:
Pax = context.Reservations.Where(y => y.Date == e.Key.Date).Sum(h => h.TotalPersons)
You're filtering exclusively by Date, but you need to filter simultaneously by Destination. As you didn't share the models it's not very easy to infer, but I believe you'll need to do the following:
Pax = context.Reservations
.Where(y => y.Date == e.Key.Date && y.Destination.Id == e.Key.Id).Sum(h => h.TotalPersons)
{
"Id": 1234,
"CommentType": "project",
"EntityReferenceId": "1345-154-154",
"Members": [{
"MemberId": "1354",
"Name": "a",
"Email": "cdc#df.com"
}],
"Threads": [{
"Id": "233",
"UserReferenceId": "32343",
"UserName": "433434",
"CommentByUserType": "Agent",
"Content": "dfdfdsfs sfdf sdf",
"PostedDate": "0001-01-01T00:00:00",
"Active": true,
"Attachment": [{
"AttachmentName": "ad",
"AttachmentUrl": "http://fdf.jpg"
}]
},
{
"Id": "233",
"UserReferenceId": "32343",
"UserName": "433434",
"CommentByUserType": "Agent",
"Content": "dfdfdsfs sfdf sdf",
"PostedDate": "0001-01-01T00:00:00",
"Active": false,
"Attachment": [{
"AttachmentName": "ad",
"AttachmentUrl": "http://fdf.jpg"
}]
}]
}
I am using MongoDb to linq,
This is my Comment" object format.for each Project there is a comment object.Each Comment object contains a list of "Threads"(you can see this above example).I want to load the "Comment" object with all thread which is Active("Active": true)
var result = _context.Comments
.AsQueryable()
.Where(x => x.EntityReferenceId == EntityReferenceId &&
x.CommentType == Type &&
x.Threads.Any(z=>z.Active==true))
.FirstOrDefault();
I used this query but it loading all threads if Any thread have "Active" value is true.
x.Threads.Any(z=>z.Active==true)
returns only bool values.I need a solution please
You can use x.Threads.Any(z=>z.Active==true) be where condition.
or you will judge Threads collection is contain Active==true data.
A simple way you can do like this.
var result = _context.Comments
.AsQueryable()
.Where(x => x.EntityReferenceId == EntityReferenceId &&
x.CommentType == Type)
.FirstOrDefault();
if (result!=null)
{
result1.Threads = result1.Threads.Where(z => z.Active == true);
}
Or
use select method
var result = _context.Comments
.AsQueryable()
.Where(x => x.EntityReferenceId == EntityReferenceId &&
x.CommentType == Type)
.Select(o => new Comment{
Id = o.Id,
Members = o.Members,
EntityReferenceId = o.EntityReferenceId,
CommentType = o.CommentType,
Threads = o.Threads.Where(z => z.Active == true)
})
.FirstOrDefault();
How can i convert DSL query below into c# NEST query?
GET project/_search
{
"size": 0,
"aggs": {
"group_by_projectId": {
"filter": {
"terms": {
"projectId.keyword": ["1", "2", "18"]
}
},
"aggs": {
"project_tags": {
"terms": {
"field": "projectId.keyword",
"size": 100
},
"aggs": {
"last_process_time": {
"max": {
"field": "processedAt"
}
}
}
}
}
}
}
}
would anyone helpy me with the nest query? thank you in advance.
i have converted the query as follows and it works perfectly fine:
_elasticClient.SearchAsync<Project>(s => s
.Size(0)
.Aggregations(a => a
.Filter("filter_by_projectId", f => f
.Filter(ff => ff
.Terms(t => t
.Field(tf => tf.ProjectId)
.Terms(projects
.Select(ps => ps.Id))))
.Aggregations(agg => agg
.Terms("group_by_projectId", st => st
.Field(o => o.ProjectId.Suffix("keyword"))
.Size(100)
.Aggregations(aa => aa
.Max("last_process_time", sa => sa
.Field(o => o.ProcessedAt))))))));
having a problem with my elasticsearch.
Setup: Having a Company-Class with the data field "companyName".
My search shall search and response all companys with the searched term.
If I try to sort via
.Sort(x=> x.OnField(x => x.CompanyName).Descending())
The data aren't sorted rightly - reference stackOverflow
I tried the given solution, but if I set my companyName to "not_analyzed" I cant even search anymore for the comnpany name or like a beginning "goo" (google)
So I tried to set up a multifield mapping, with a suffix, which isn't analyzed and one which is analyzed.
My Index is set up like:
client.CreateIndex(IndexName, c => c
.AddMapping<Exhibitor>(m =>m
.MapFromAttributes()
.Properties(o => o
.MultiField(mf=>mf
.Name(x=>x.CompanyName)
.Fields(fs => fs
.String(s=>s.Name(t=>t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
.String(s=>s.Name(t=>t.CompanyName.Suffix("raw")).Index(FieldIndexOption.NotAnalyzed))))
)
)
)
);
My search looks like:
string SearchTerm ="my search term"
results = GetClient().Search<Company>(s => s
.Query(qa => qa
.MatchPhrasePrefix(m => m
.OnField(f=>f.CompanyName)
.Query(SearchTerm)
))
.Sort(x => x.OnField(x => x.CompanyName.Suffix("raw")).Descending())
.Size(maxResults).Skip(page * pageSize).Take(pageSize)
);
But that still doesn't work.
Any ideas?
Thanks in advance.
Update 1:
For case insensitive sorting I added an custom analyzer:
var companyAnalyzer = new CustomAnalyzer
{
Filter = new List<string> { "standard", "lowercase" },
Tokenizer = "keyword"
};
client.CreateIndex(IndexName, c => c
.Analysis(analysis => analysis
.Analyzers(a => a
.Add("companyanalyzer", companyAnalyzer)
)
)
.AddMapping<Exhibitor>(m => m
.MapFromAttributes()
.Properties(o => o
.MultiField(mf => mf
.Name(x => x.CompanyName)
.Fields(fs => fs
.String(s => s.Name(t => t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
.String(s => s.Name(t => t.CompanyName.Suffix("raw")).Index(FieldIndexOption.Analyzed).Analyzer("companyanalyzer"))))
)
)
);
This example is working, maybe it will put some light on your issue.
var indicesResponse = client.DeleteIndex(descriptor => descriptor.Index(indexName));
client.CreateIndex(indexName, c => c
.AddMapping<Exhibitor>(m => m
.MapFromAttributes()
.Properties(o => o
.MultiField(mf => mf
.Name(x => x.CompanyName)
.Fields(fs => fs
.String(s => s.Name(t => t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
.String(s => s.Name(t => t.CompanyName.Suffix("raw")).Index(FieldIndexOption.NotAnalyzed))))
)));
client.Index(new Exhibitor { Id = 1, CompanyName = "a test" });
client.Index(new Exhibitor { Id = 2, CompanyName = "b test" });
client.Index(new Exhibitor { Id = 3, CompanyName = "c test" });
client.Refresh();
string SearchTerm = "test";
var results = client.Search<Exhibitor>(s => s
.Query(qa => qa
.MatchPhrasePrefix(m => m
.OnField(f => f.CompanyName)
.Query(SearchTerm)
))
.Sort(x => x.OnField(f => f.CompanyName.Suffix("raw")).Descending())
);
Result of this query:
{
"took": 2,
"timed_out": false,
"_shards": {..}
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "my_index",
"_type": "exhibitor",
"_id": "3",
"_score": null,
"_source": {
"id": 3,
"companyName": "c test"
},
"sort": [
"c test"
]
},
{
"_index": "my_index",
"_type": "exhibitor",
"_id": "2",
"_score": null,
"_source": {
"id": 2,
"companyName": "b test"
},
"sort": [
"b test"
]
},
{
"_index": "my_index",
"_type": "exhibitor",
"_id": "1",
"_score": null,
"_source": {
"id": 1,
"companyName": "a test"
},
"sort": [
"a test"
]
}
]
}
}
Index mapping:
{
"my_index" : {
"mappings" : {
"exhibitor" : {
"properties" : {
"companyName" : {
"type" : "string",
"analyzer" : "standard",
"fields" : {
"raw" : {
"type" : "string",
"index" : "not_analyzed"
}
}
},
"id" : {
"type" : "integer"
}
}
}
}
}
}
Hope it helps.
Suppose I have a List of dynamic objects like:
var records = [
{
"Id": 1,
"Name": "sai",
"Age": "4",
"About": "12.02.1991"
},
{
"Id": 2,
"Name": "hjfh",
"Age": "2",
"About": "12.02.1991"
},
{
"Id": 3,
"Name": "hello name",
"Age": "6",
"About": "hi"
},
{
"Id": 4,
"Name": 1,
"Age": "9",
"About": "hello world"
}
]
string searchString = "Hello";
I tried something like:
foreach (var item in records )
{
foreach (var field in item)
{
if (field.Value != null && field.Value.ToString().IndexOf(query.SearchString, StringComparison.OrdinalIgnoreCase) >= 0)
{
count++;
break;
}
}
}
I want the count of records which has searchString at any field in the record. but can I write this using a LINQ query?
This is your code, converted to a linq expression:
You have access to the field and the object in the select statement.
records.Cast<ExpandoObject>().SelectMany(x => x, (obj, field) => new { obj, field })
.Where(x => x.field.Value != null && x.field.Value.ToString().IndexOf(SearchString, StringComparison.OrdinalIgnoreCase) >= 0)
.Select(x => x.field.Key);
To count the number of objects that have at least one field that contains the searchstring:
records.Cast<ExpandoObject>()
.Where(x => x.Any(y => y.Value != null && y.Value.ToString().IndexOf(SearchString, StringComparison.OrdinalIgnoreCase) >= 0))
.Count();
It would be something like this:
logic:
var qry = records.Where(x=>x.Field.Contains(searchedstring)).Count();
real example:
var qry = records.Where(x=>x.Name.Contains("sai")).Count();
Below is the simplest code.
int count = records.Count(x => x.Name.StartsWith("hello") || x.About.StartsWith("hello"));