I tried on two ways to choose to date only from the date frame but still, I get the wrong result. I saw another post but the response doesn't work for me. Please help mine.
List<Measurement> measurementsForTemplate = await databaseCommandContext.CreateSet<Measurement>()
.Include(v => v.MeasuredValues)
.Where(x => x.MeasurementFormTemplateId == command.TemplateId )
.Where(v => v.MeasuredValues.Any(d => d.Time >= command.FromDate))
.Where(v => v.MeasuredValues.Any(d => d.Time <= command.ToDate)).ToListAsync(cancellationToken);
Or I created an object without filtering in the above query and then
var measurementsForTemplateDateLimit = measurementsForTemplate.Where(b => b.MeasuredValues
.Any(d => d.Time >= command.FromDate && d.Time <= command.ToDate)).Select(b => b);
See figure below with solution
Related
I'm trying to work out how best to filter Posts by their Fields/Regions in Piranha without loading them all and then performing the filtering.
At the moment I have the following:
var posts = _db.Posts
.Where(p => p.Published >= DateTime.Now)
.Where(x => x.title == "somestring")
.Select(p => p.Id)
.ToList();
If I wanted to query a field in regions named FooArticleDetails how would I modify the above to achieve this?
I can retrieve the results by loading all the Posts and their related Regions but this is not preferred for obvious reasons.
loadedItems.Posts
.Where(post =>
suppliedDiff.Contains(
"hard",
post.Regions.FooArticleDetails.difficulty.Value)).ToLower()
))
.ToList();
I've figured it out. Post Regions are stored as PostFields. These can be accessed via the Fields property.
posts.Where(post =>
post.Fields
.Where(x =>
x.FieldId == "difficulty" &&
x.RegionId == "FooArticleDetails" &&
suppliedDiff.Contains(x.Value.ToLower())
)
.Any());
I am trying to search records in Nest, my conditions are, expiration_date can be null or it can be within some date(ex. 10-20-2018) and effective_date can be some date (09-20-2018).
Below is my query, here I am not able to use || and && operator, either syntax issue, or my approach is wrong, can anyone help me with this?
docs = await _client.SearchAsync<PriceList>(s => s.Index(config.elasticsearchIndex)
.Query(a => a.Bool(c=>c.Should(
d => d.Bool(e => e.MustNot(f=>f.Exists(g => g.Field(h => h.ExpirationDate))))
||
.Query(a => a.DateRange(r => r.Field(field => field.ExpirationDate).GreaterThanOrEquals(forThisRange.fromDate)))
)))
.Query(a => a.DateRange(r => r.Field(field => field.EffectiveDate).LessThanOrEquals(forThisRange.toDate)))
My nest version is 6.4
UPDATED Query:
.Query(a => a.Bool(c=>c.Should(
d => d.Bool(e => e.MustNot(f=>f.Exists(g => g.Field(h => h.ExpirationDate))))
,
d=>d.Bool(e=>e.Must(f=>f.DateRange(r => r.Field(field => field.ExpirationDate).GreaterThanOrEquals(forThisRange.fromDate))))
//i=>i.DateRange
)))
.Query(a => a.DateRange(r => r.Field(field => field.EffectiveDate).LessThanOrEquals(forThisRange.toDate)))
I am not getting any error, but not getting extra record,it is giving correct result + "expiration_date" has less than 10-20-2018,the later should not.
Thanks all for your comments.
I found the solution, replace .Query() with .PostFilter() which resolved the issue. Don't know the exact difference between them, but it worked.
If anybody knows it, please comment it, may be helpful to others.
I got records in database. Each record has field "Date".
For given date=x i need to find records that Date value is less that x, but next record date is greater of x.
Example:
id date
--------------
1 12.03.2013
2 15.03.2013
3 18.03.2013
now, I got X=16.03.2013, and i need a LINQ that return this record:
2 15.03.2013
BUT! for the X=15.03.2014 it should return nothing (because there is record with smaller date, but next record has exactly the same date as X)
How can i do this?
The simplest approach IMO is just to find the record that it would find, and check the date afterwards:
var result = db.Table
.Where(x => x.Date <= target.Date)
.OrderByDescending(x => x.Date)
.FirstOrDefault();
if (result != null && result.Date == target.Date)
{
result = null;
}
Or you could do it all in the query using a secondary Where clause after filtering to a single result:
var result = db.Table
.Where(x => x.Date <= target.Date)
.OrderByDescending(x => x.Date)
.Take(1)
.Where(x => x.Date != target.Date)
.FirstOrDefault();
Note that this doesn't work if all values are less than x (so there's no "next" record at all). I haven't yet worked out a way to handle that.
If all values can be less than x then you can use
var candidate = dates.Where(x => x.Date < target.Date)
.OrderByDescending(x => x.Date)
.FirstOrDefault();
var next = dates.Where(x => x.Date >= target.Date)
.OrderBy(x => x.Date)
.FirstOrDefault();
return (candite != null && next != null && next.Date != target.Date) ? candidate : null
I'm not sure how to do this with the entity framework. I got the following:
return this.enrollments
.Where(e => e.em_enrolled == false && e.em_result < _settings.PassMark)
.GroupBy(e => e.em_subject_id)
.Select(e => e.em_subject_id)
.ToList();
how do i only retrieve records that are present x times.
Do you mean groups with x or more items?
return this.enrollments
.Where(e => e.em_enrolled == false && e.em_result < _settings.PassMark)
.GroupBy(e => e.em_subject_id)
.Where(g => g.Count() >= x)
.Select(g => g.Key)
.ToList();
I suspect you want:
return this.enrollments
.Where(e => !e.em_enrolled && e.em_result < _settings.PassMark)
.GroupBy(e => e.em_subject_id)
.Where(g => g.Count() >= x)
.Select(g => g.Key)
.ToList();
Note that I've changed the Select part to reflect the fact that you want to extract the group key from the group. (I've also avoided comparison with false, changing e.em_enrolled == false into !e.em_enrolled. They mean the same thing of course, but I find the latter more idiomatic in C#.)
We're using a database that doesn't support query batching so we can't make use of NHibernate Futures in this instance. We know we can still populate our complex object graph using multiple-queries (to avoid Cartesian products) but need advice if I can refactor the approach.
Please note > Sole developer here, so seeking advice.
Here is some sample code which illustrates the current approach;
var fruitBasketAlias = new Store.FruitBasket();
var yoghurtAlias = new Store.Yoghurt();
var flavourAlias = new Store.Flavour();
var ownersAlias = new Store.Owner();
var FruitBaskets = session.QueryOver(() => fruitBasketAlias)
.Where(() => fruitBasketAlias.Owner.ID == OwnerID
&& fruitBasketAlias.ExpiryDate <= dateTO
&& fruitBasketAlias.ExpiryDate >= dateFROM)
.Fetch(x => x.BasketLiner).Eager
.List();
session.QueryOver(() => fruitBasketAlias)
.Select(x => x.Yoghurts)
.Where(() => fruitBasketAlias.Owner.ID == OwnerID
&& fruitBasketAlias.ExpiryDate <= dateTO
&& fruitBasketAlias.ExpiryDate >= dateFROM)
.JoinAlias(() => fruitBasketAlias.Yoghurts, () => yoghurtAlias, JoinType.LeftOuterJoin)
.JoinAlias(() => yoghurtAlias.Flavour, () => flavourAlias, JoinType.InnerJoin)
.List();
session.QueryOver(() => fruitBasketAlias)
.Where(() => fruitBasketAlias.Owner.ID == OwnerID
&& fruitBasketAlias.ExpiryDate <= dateTO
&& fruitBasketAlias.ExpiryDate >= dateFROM)
.JoinAlias(() => fruitBasketAlias.Yoghurt, () => yoghurtAlias, JoinType.LeftOuterJoin)
.JoinAlias(() => yoghurtAlias.Flavour, () => flavourAlias, JoinType.InnerJoin)
.JoinAlias(() => flavourAlias.Owners, () => ownersAlias, JoinType.LeftOuterJoin)
.List();
You can see from the code above that I am using three separate queries to populate a list of FruitBaskets. This approach is working but I suspect there is a better way to join all the children into the parent object without having to query from the root object each time.
Is there an approach I can use which will enable me to apply the where condition to the parent object and use the results of that query to automatically obtain all the children objects. Please note that children can go 3 levels deep, i.e. FruitBasket.Yoghurt.Flavour.Owners.
Any advice is appreciated.
C# .NET 4, NHibernate 3.0
var FruitBaskets = session.QueryOver<FuitBasket>()
.Where(b => b.Owner.ID == OwnerID
&& b.ExpiryDate <= dateTO
&& b.ExpiryDate >= dateFROM)
.Fetch(x => x.BasketLiner).Eager
.JoinAlias(b => b.Yoghurts, () => yoghurtAlias, JoinType.LeftOuterJoin)
.List();
var yoghurts = session.QueryOver<Store.Yoghurt>()
.WhereRestrictionOn(y => y.Id).In(FruitBaskets.SelectMany(b => b.Yoghurts).Select(y = > y.Id).Distinct())
.JoinAlias(y => y.Flavour, () => flavourAlias, JoinType.InnerJoin)
.List();
session.QueryOver<Store.Flavor>()
.WhereRestrictionOn(f => f.Id).In(yoghurts.SelectMany(y => y.Flavors).Select(b = > f.Id).Distinct())
.JoinAlias(f => f.Owners, () => ownersAlias, JoinType.LeftOuterJoin)
.List();
couldn't test it though