How to improve my LINQ

Matt

The following query does solve my problem however it seems overly complex.

The problem is I have a table that stores menu options (id, name, description and price). I also have a session variable that stores the users selections in a Dictionary (storing id and quantity).

The LINQ I've produced below basically calculates the value of everything summed up. It converts the table to a dictionary and then sums the prices multiplied by the quantity.

Is there a better way to do this?

var prices = _db.MenuOptions
                .Select(o => new { o.Id, o.Price })
                .ToDictionary(o => o.Id, o => o.Price);
Session["price"] = prices
              .Where(p => orderItems.Keys.Contains((int)p.Key))
              .Sum(p => p.Value * orderItems[p.Key]);
Raidri supports Monica

The select part is unnessecary and can be dropped:

var prices = _db.MenuOptions.ToDictionary(o => o.Id, o => o.Price);

And the price calculation could start from the orderItems:

Session["price"] = orderItems.Sum(oi => oi.Value * prices[oi.Key]);

(Assuming all orderItems have a price in the db.)

Edit: Going from Arcturus answer, something like this might also be possible for a "one-liner" but is probably slower (see comments):

Session["price"] = orderItems.Sum(oi => oi.Value * _db.MenuOptions.Single(o => o.ID == oi.Key).Price);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I improve LINQ?

From Dev

How can I improve LINQ?

From Dev

can my code improve from using LINQ?

From Dev

How to improve my REST API?

From Dev

How to improve performance of my laptop?

From Dev

How can i improve the performance of this LINQ?

From Dev

How to improve this C# Linq code?

From Dev

How to improve LINQ repository query in MVC 4

From Dev

How to improve the speed of my selection process, python

From Dev

How to improve my software analysis and UML skills

From Dev

How can I improve the performance of my script?

From Dev

css responsive design: how to improve my page ?

From Dev

How to improve my natural language search query

From Dev

How to improve my software analysis and UML skills

From Dev

How would move semantics improve "my way"?

From Dev

How can I improve my linux security?

From Dev

How to improve my FindDuplicate classic algorithm

From Dev

How to improve my ios app performance

From Dev

How to improve my "random words cloud" function?

From Dev

How to improve efficiency of my redirect code

From Dev

How to improve my feature selection for a NB classifier?

From Dev

How can I improve my android layout

From Dev

how to improve my "String" object comparing function

From Dev

How does my VPN improve my download speed?

From Dev

How to improve my if else statement for my additional program function

From Dev

How to improve performance using declarative manner instead of LINQ-to-Objects?

From Dev

Optimizing LINQ queries - How can I improve the execution time?

From Dev

Improve LINQ performance

From Dev

Improve LINQ query performance?