score:6
well for one thing you can speed things up using joins, and only evaluating the valid "target" currencies once:
var targetrates = amdw.fxrates
.where(rate => rate.basecurrency == tocurrency)
.tolist();
var query = from holding in holdings
join fxrateholding in amdw.fxrates
on new { holding.currency, holding.date } equals
new { currency = fxrateholding.basecurrency,
date = fxrateholding.valuedate }
join fxratetocur in targetrates
on holding.date equals fxratetocur.valuedate
select new { holding, fxrateholding, fxratetocur };
personally i wouldn't try to update the list in place and i wouldn't mutate the existing holdings (as you currently do in your select
call). mutating existing values tends to make your code harder to reason about - which is why linq is designed to be used in a more functional way.
score:0
well you could do it in sql, just note that not everyone will expect side affects in linq, but after all c# is not a functional language.
here is what you could do:
for updating a single property just do:
from a in ab select a.prop = newval;
for updating multiple properties, you have the following options:
split the updates in multiple linq queries, each updating a single property (obviously not the best idea)
do the following
from a in ab select new { x = a.prop = newval, y = a.prop2 = newval2 };
in a linq method chain (but not in a query expression) you could do
ab.select( a=> { x = a.prop = newval; y = a.prop2 = newval2; return x; };
(actually in linq to objects update there is a suggestion to refactor this last method into an extension method, which will allow it to use in a query expression, as well as clarify the intention).
score:2
you shouldn't use linq when updating objects. linq is supposed to be side-effect free, you should use it to get the items you want to change then do a foreach on them.
foreach(var dhi in query)
{
decimal factor = dhi.fxratetocur.value / dhi.fxrateholding.value;
dhi.holding.marketvalue *= factor;
dhi.holding.fee *= factor;
dhi.holding.remuneration *= factor;
}
you could also use foreach function defined on list<t>
like this.
query.tolist().foreach(dhi =>
{
decimal factor = dhi.fxratetocur.value / dhi.fxrateholding.value;
dhi.holding.marketvalue *= factor;
dhi.holding.fee *= factor;
dhi.holding.remuneration *= factor;
});
like jeff mercado pointed out in comments, it's not a good idea to make a list just to call foreach()
on it.
Source: stackoverflow.com
Related Query
- LINQ to Objects, how to update items within List in place?
- How to search any items of a list within a string using Linq
- how to perform a bulk update with a list of objects linq c#
- How to select values within a provided index range from a List using LINQ
- LINQ - Get all items in a List within a List?
- How to update value in a List using LINQ
- Using LINQ to split items within a list
- Linq - How to select items from a list that contains only items of another list?
- How to update an element with a List using LINQ and C#
- LINQ Getting Unique items from a List within a List
- How to update a single item of liist of objects using LINQ in C#
- Using Linq, how to GroupBy against objects in a list within the target object
- How to convert list of objects with two fields to array with one of them using LINQ
- Linq return parent objects that have child items matching ALL items in separate list
- How to generate a unique list of items from another list using LINQ in C#
- linq - how do you do a query for items in one query source that are not in another one?
- how do you execute a function on a List of objects using LINQ
- Using LINQ I have a list of lists, how do I select all objects that exist in every list?
- How can I use LINQ to return a list of Countries but place a particular country aribitrarly at the top?
- How to get a complement list of objects using linq and EntityFramework
- How to change NOCOUNT setting within LinqToSQL because of ChangeConflictException in Linq to Sql update
- Linq to select list of items from custom nested list of objects
- Linq to Objects - Where search within a list
- How to include only some items of internal list with LINQ to Entities
- How can you use LINQ to cast from a collection of base class objects to a filtered list of their respective subclasses?
- How to return list of items from linq to sql query and call it in a console application
- Update list within list using linq
- How can I obtain objects from list with C# LINQ using boolean conditions?
- Linq to NHibernate: how to get a list of child objects without having the lists nested
- How to get all Objects from a List in a List with LINQ
More Query from same tag
- Linq group by + where for each group
- How do I do this in Entity Framework (multiple where's or Join)?
- Cannot convert List<model> to model
- Passing data in ASP.NET MVC using LINQ - nuttiness
- Group by a collection as the key
- Linq for NHibernate - filtering on <many-to-one> foreign key causes extra lookup
- Get a list of entities by list of guids
- Creating a property selector Expression from a string
- Linq for objects
- How to reuse a data into an anonymous type decalaration?
- LINQ to SQL where in (lambda syntax)
- How can I move on column data to another column in the same row using linq
- How can i Insert into one table and Update another table using Linq
- Delete Old Items from Product List Using LINQ
- List.OrderBy returns a collection of empty strings
- LINQ - Creating Subquery based on Where clause
- Linq-to-Entities: My OrderBy clause isn't ordering
- How can I group by specific timestamp intervals in C# using LINQ?
- How to use linq to paginate a SortedList in Ascending or Descending order
- How to skip repeated ids from table and select remains using linq
- Cannot implicitly convert type 'System.Collection.Generic.IEnumerable<string>' to 'string' while Adding data to the ViewModel
- LINQ where not between a date range
- Nhibernate and Query example
- how to use substring in ef query within where and select statement
- Retrieving specific elements from an XDocument based on a list
- NullReferenceException when Linq'ing IEnumerable<XElement>
- how do I make changes to value in query
- How to Insert into Identity column with c# code
- Get record index column in linq select
- Filtering a collection based on filtering rules