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: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)
score:1
i think better approach would be to deserialize xml to c# classes and then use linq on that, should be fast.
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)
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- VB.NET LINQ Method Syntax disallows implicit conversions from 'Integer?' to 'Integer'
- LINQ search though a list of string arrays for a particular string
- LINQ to Entities does not recognize the method, how to fix
- How can I find object in List with Linq?
- Mapping a database value to a TimeSpan using dbml
- Why is Distinct not working in this LINQ query?
- How to get first username in database with linq?
- Linq Get totals using group by
- Generic way to find or create an object in LINQ to SQL?
- Select Distinct records by IDictionary<string,object> property in an object
- Grouping value from left outer join
- Linq Using Contains On An Array
- LINQ to DATASET with Group Join and Group By (vb.net)
- Converting a given SQL Query to LINQ
- how to get all non null values from array in c#
- Compare two lists of strings of varying length for difference based on content, not length
- Using LINQ to exclude a Child Node from a collection of the same name
- how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#
- What if IEnumerable<string> returns just one string
- Dynamic Data tables joining Using Linq
- Dependancy Property with Observable Collection
- C# Retrieving the absolute value closest to 0 from a sum in a list
- Sorting the items of a recursive list with "infinite" levels/generations
- Filter c# list using timestamp, Take first record of each 5 seconds
- Linq to NHibernate returns different results than HQL?
- Double filter with lambda expression
- How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
- Why LastOrDefault not working in asp.net linq?
- Group a collection and take first 5 from each group
- linq query for tag system - search for multiple tags