score:1
i think better approach would be to Deserialize XML to C# Classes and then use LINQ on that, should be fast.
score:0
If it really takes this long to run this, then maybe do something like this:
- Don't iterate both - only iterate the XML-File and load the Data from your DataLst (make a SQL-command or simple linq-statement to load the data based on Name/Store/Section), make a simple struct/class for your key with this data (Name/Store/Section) - don't forget to implement equals, and GetHashCode
- iterate through your XML-Elements and use the dictionary to find the values to replace
This way you will only iterate the XML-File once not once for every data in your DataSource.
score:2
It looks like you have an O(n)×O(m) issue here, for n = size of DataList and m = size of the xml. To make this O(n)+O(m), you should index the data; for example:
var lookup = elements.ToLookup(
x => new {
Name = x.Name.LocalName,
Store = x.Element(XName.Get("Store", "")).Value,
Section = x.Element(XName.Get("Section", "")).Value},
x => x.Element(XName.Get("Val", ""))
);
foreach (DataSource Data in DataLst)
{
XElement xmlElem = lookup[
new {Data.Name, Data.Store, Data.Section}].Single();
xmlElem.ReplaceWith(new XElement(XName.Get("Val", ""), Data.Value));
}
(untested - to show general approach only)
score:0
It's not clear why it's taking that long - that's a very long time. How many elements are in DataLst
? I would rewrite the query for simplicity to start with though:
IEnumerable<XElement> elements = xmlDoc.Descendants();
foreach (DataSource data in DataLst)
{
XElement valElement = (from element in xmlDoc.Descendants(data.Name)
where data.Store == element.Element("Store").Value
&& data.Section == element.Element("Section").Value
select element.Element("Val")).Single();
valElement.ReplaceWith(new XElement("Val"), data.Value));
}
(I'm assuming none of your elements actually have namespaces, by the way.)
Next up: consider replacing the contents of valElement
instead of replacing the element itself. Change it to:
valElement.ReplaceAll(data.Value);
Now, this has all been trying to keep to the simplicity of avoiding precomputation etc... because it sounds like it shouldn't take this long. However, you may need to build lookups as Marc and Carsten suggested.
score:0
Try by replacing Single()
call in the LINQ with First()
.
score:0
At the risk of flaming, have you considered writing this in XQuery instead? There's a good chance that a decent XQuery processor would have a join optimiser that handles this query efficiently.
score:0
"Well thanks everyone for your precious time and effort"
Problem answer: Actually the object 'DataLst' was of the type IEnumerable<> which was taking time in obtaining the values but after I changed it to the List<> type the performance improved drastically (now running in 20 seconds)
Source: stackoverflow.com
Related Articles
- Performance issue with Linq to object taking more time
- Using multiple Contains taking time to load performance issue in Linq
- Linq with where clause in many-to-many EF Code First object
- EntityFramework 6.1.1 with Linq Performance issue
- Linq query performance with new object in `Where`
- Linq to sql as object data source - designer problem with partial classes
- Accessing SQL Server time in code with LINQ
- LINQ to Entities performance issue with Where and Contains
- Parallel LINQ GroupBy taking long time on systems with high amount of cores
- Issue with Object returned when using Linq Where clause
- Performance issue with LINQ when getting recent data by hours on a large set
- Linq query taking a long time to query a table with 6000 records
- ASP.NET MVC LINQ Performance issue with two databases
- LINQ DefaultIfEmpty issue on object with non-nullable value type property
- Linq Query Where() with Date subtraction taking excessive time
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Performance issue with linq query
- Linq performance issue with huge record set
- .NET Core / EF : how to avoid performance issue with Linq query?
- How can I return two fields using Linq on an object with more than two fields?
- LINQ and SQL performance issue when working with Membership
- LINQ to xml query with performance issue
- Performance issue in IEnumerable type when querying large amount of data with LINQ
- Performance issue with Linq query - navigation properties to blame?
- How to use LINQ to select object with minimum or maximum property value
- Create a list from two object lists with linq
- LINQ OrderBy with more than one field
- LINQ to SQL: GroupBy() and Max() to get the object with latest date
- How to handle NULL object property with FirstOrDefault using Linq
- C# Linq intersect/except with one part of object
- C# generic method for multiple classes
- Search for null objects inside array of objects using LINQ
- Entity Framework - Remove List of Items
- Linq to SQL, InsertOnSubmit vs. InsertAllOnSubmit performance?
- EF/Linq enum error
- ParallelQuery with methods that take in an IEnumerable
- Weird exceptions compiled dynamically built expression
- Creating a queryable cached result from LINQ
- Dynamicly pass table name to LINQ
- Cross Context LINQ Joins in Azure?
- Entity Framework create update query unnecessary
- C# nullable and FirstOrDefault failure
- Nested LINQ Query Question
- String.Split in a Linq-To-SQL Query?
- Get all Image Elements within a Grid->Canvas using LINQ?
- Difference between expression lambda and statement lambda
- Linq inner join grouping
- increase Performance of the code
- MySQL LINQ to SQL Provider Visual Studio 2012
- linq - use property from parent object within Any() call