"Nested foreach" vs "lambda/linq query" performance(LINQ-to-Objects)
Tag : chash , By : Alex Bartzas
Date : March 29 2020, 07:55 AM
this will help Write the clearest code you can, and then benchmark and profile to discover any performance problems. If you do have performance problems, you can experiment with different code to work out whether it's faster or not (measuring all the time with as realistic data as possible) and then make a judgement call as to whether the improvement in performance is worth the readability hit. A direct foreach approach will be faster than LINQ in many cases. For example, consider: var query = from element in list
where element.X > 2
where element.Y < 2
select element.X + element.Y;
foreach (var value in query)
{
Console.WriteLine(value);
}
foreach (var element in list)
{
if (element.X > 2 && element.Y < 2)
{
Console.WriteLine(element.X + element.Y);
}
}
var query = from item in firstSequence
from nestedItem in item.NestedItems
select item.BaseCount + nestedItem.NestedCount;
|
LINQ to SQL: filter nested objects with soft deletes
Tag : chash , By : bikefixxer
Date : March 29 2020, 07:55 AM
I wish this helpful for you So far I found only one suitable solution for this (apart from not to use soft deletes at all): to delete the soft-deletion relationship on entity update. E.g. when I decided to remove a file from the document, I perform something like: // this may be a part of update method
var file = document.File;
if (file.IsDeleted)
{
// attach soft deleted file
context.Files.Attach(file, true);
// remove a file reference
document.File = null;
}
// attach document entity and submit changes
context.Documents.Attach(document, true);
context.SubmitChanges();
|
Rewrite nested foreach with linq if there are some operations apart from internal foreach loops
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Whole code is quite suspicious as variable cT is only added to List, neither of it`s properties is checked inside foreach loop. Either this is original behaviour or consequence of obfuscation, you should revise your sample. List<complexType> listOfComplex = ...some list...
//List<complexType> newListOfCOmplex = new List<complexType>();
SomeObjectType someObject = ...some object...
//Totaly unneccessary as newListOfCOmplex is complete copy of listOfComplex
//foreach(var cT in listOfComplex)
//{
// newListOfCOmplex.Add(cT);
//}
var someObjectPropertyValue = someObject.property.FirstOrDefault(a=>a.value == smth) ?? return null;
var t = someObjectPropertyValue.Something.AnotherSomethin ?? return smth;
var collection = t.Where(s=>(s.value == firstValue || s.value == secondValue) ).ToList();
foreach (var f in collection) someOtherMethod(f);
return smth;
}
|
Query/Filter by nested objects getting unexpected results in some nested objects
Date : March 29 2020, 07:55 AM
I hope this helps . From the mapping it looks like the index has a type of category as well as a field in attribute type called category. To allow for proper field resolution and disambiguate between field id in type category vs field category.id in type attribute you would need to specify the entire path to the field including the type i.e ..POST /attribute-tree/_search
{
"size": 2,
"query": {
"filtered": {
"filter": {
"term": {"attribute.category.id": 1}
}
}
}
}
|
Nested LINQ queries to filter objects by its sub objects properties
Tag : chash , By : Josh Tegart
Date : March 29 2020, 07:55 AM
hope this fix your issue You need to create a pair of the original Node and the Path matching the string, so you can order the results and get back the node afterwards Nodes
.Select(n => new { Node = n, Path = n.Paths.FirstOrDefault(p => p.item1 == s) })
.Where(x => x.Path != null)
.OrderBy(x => x.Path.item2)
.Select(x => x.Node)
|