473,241 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Multiplication using Linq

Hello,

Is it possible to multiply all Prices in a List<Productby 1.1 using
Linq?

Product has a property named Price.

Thanks,
Miguel
Sep 4 '08 #1
7 11495
On Thu, 04 Sep 2008 15:06:52 -0700, shapper <md*****@gmail.comwrote:
Is it possible to multiply all Prices in a List<Productby 1.1 using
Linq?

Product has a property named Price.
All due respect, not every problem is best solved using LINQ. Sometimes,
just a normal "foreach" enumeration is best.

If you are trying to query a set of data (collection, database, etc.) and
manipulate the results of such a query, then LINQ can be very useful. But
if you're just trying to process individual elements in a set of data,
using LINQ could be counter-productive.

No offense intended, but your posts seem to have this sort of "vibe" of
someone who's found a shiny new hammer called "LINQ" and now thinks all of
his problems are nails. :)

At the very least, it might be helpful to those trying to answer if you
could explain why all of your questions look like "Is it possible to do X
using LINQ?" Why is it that you want to implement all of these different
things with LINQ? What's wrong with just doing it "the old fashioned way"?

Pete
Sep 4 '08 #2
shapper wrote:
Is it possible to multiply all Prices in a List<Productby 1.1 using
Linq?

Product has a property named Price.
I would use:

list.ForEach((Product p) =p.Price *= 1.1m);

Arne
Sep 5 '08 #3
On Sep 4, 11:18*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 04 Sep 2008 15:06:52 -0700, shapper <mdmo...@gmail.comwrote:
Is it possible to multiply all Prices in a List<Productby 1.1 using
Linq?
Product has a property named Price.

All due respect, not every problem is best solved using LINQ. *Sometimes, *
just a normal "foreach" enumeration is best.

If you are trying to query a set of data (collection, database, etc.) and*
manipulate the results of such a query, then LINQ can be very useful. *But *
if you're just trying to process individual elements in a set of data, *
using LINQ could be counter-productive.

No offense intended, but your posts seem to have this sort of "vibe" of *
someone who's found a shiny new hammer called "LINQ" and now thinks all of *
his problems are nails. *:)

At the very least, it might be helpful to those trying to answer if you *
could explain why all of your questions look like "Is it possible to do X*
using LINQ?" *Why is it that you want to implement all of these different *
things with LINQ? *What's wrong with just doing it "the old fashioned way"?

Pete
But does now Linq in some cases convert the code to a Loop itseld?

This was one idea I got from this forum ... So when using Lists Linq
can be useful in some operations ...

Thanks,
Miguel
Sep 5 '08 #4
This was one idea I got from this forum ... So when using Lists Linq
can be useful in some operations ...
Maybe - but in the /purest/ sense, LINQ is a [Q]uery language, not a
manipulation language. Note that there is no Enumerable.ForEach, for
example. Indeed the Expression side of LINQ is designed to be side-
effect free [at least, assuming that property getters are well-
behaved, etc].

There is nothing stopping you doing List<T>.ForEach([some lambda]),
but it doesn't usually gain you anything either - and will be (very
slightly) slower in the bargain (delegate invoke, capture indirection,
interface-vs-struct enumerator, etc).

Marc
Sep 5 '08 #5
On Sep 5, 6:32*am, Marc Gravell <marc.grav...@gmail.comwrote:
This was one idea I got from this forum ... So when using Lists Linq
can be useful in some operations ...

Maybe - but in the /purest/ sense, LINQ is a [Q]uery language, not a
manipulation language. Note that there is no Enumerable.ForEach, for
example. Indeed the Expression side of LINQ is designed to be side-
effect free [at least, assuming that property getters are well-
behaved, etc].

There is nothing stopping you doing List<T>.ForEach([some lambda]),
but it doesn't usually gain you anything either - and will be (very
slightly) slower in the bargain (delegate invoke, capture indirection,
interface-vs-struct enumerator, etc).

Marc
Just as a try I have the following:
Products.Select(p =p.Price = p.Price * 1.2;)

This is not working ... what am I doing wrong?

Thanks,
Miguel
Sep 5 '08 #6
"shapper" <md*****@gmail.comwrote in message
news:0a**********************************@e53g2000 hsa.googlegroups.com...
Just as a try I have the following:
Products.Select(p =p.Price = p.Price * 1.2;)
This is not working ... what am I doing wrong?
This should work, if you've got all your types right. Are you sure that
Price is not an int or decimal (1.2 is double, and you can't multiply a
decimal and a double).

That said, it's still a bad idea. Mutating data in your queries is bad for a
multitude of reasons - one of them being that it will likely simply not work
with any IQueryable that maps the query to something (I mean, think about
it - LINQ to SQL tries to map Where()/Select() to SQL SELECT - how'd you map
the one you wrote above yourself?). It will also subtly break things if you
ever try to move to Parallel LINQ. LINQ is inherently functional, if you
need to transform data using it, you should create a new sequence, not
mutate the old one. If you want to mutate in-place, use imperative
constructs such as foreach.

I would highly recommend you to read the article "Functional vs. Procedural
Programming (LINQ to XML)":

http://msdn.microsoft.com/en-us/library/bb675169.aspx

While it deals with LINQ to XML, and its samples are XML-related the general
principles outlined within are equally applicable to all other LINQ flavors.
Sep 5 '08 #7
Peter Duniho wrote:
On Thu, 04 Sep 2008 15:06:52 -0700, shapper <md*****@gmail.comwrote:
>Is it possible to multiply all Prices in a List<Productby 1.1 using
Linq?

Product has a property named Price.

All due respect, not every problem is best solved using LINQ.
Sometimes, just a normal "foreach" enumeration is best.

If you are trying to query a set of data (collection, database, etc.)
and manipulate the results of such a query, then LINQ can be very
useful. But if you're just trying to process individual elements in a
set of data, using LINQ could be counter-productive.

No offense intended, but your posts seem to have this sort of "vibe" of
someone who's found a shiny new hammer called "LINQ" and now thinks all
of his problems are nails. :)

At the very least, it might be helpful to those trying to answer if you
could explain why all of your questions look like "Is it possible to do
X using LINQ?" Why is it that you want to implement all of these
different things with LINQ? What's wrong with just doing it "the old
fashioned way"?
I was just thinking of posting the same! :)

@Shapper: take a step back, and first think how you would do it in
normal imperative (== C#) code, and then think how you might want to do
this in linq, but always consider: what you want to make is possible
without linq so using 'linq' is just another way of doing it, it's not
necessary to make things possible.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Sep 5 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
11
by: mjdeesh_hi | last post by:
How can we perfom multiplication programatically without using + or * operator. Can any one help out in this one. Jagadeesh.
18
by: martin | last post by:
I'm just wondering... Will it be possible to use C# 3.0 and Linq to objects without having our users download the .NET Framework 2.0 client? I really would like to use the C# 3.0 features, but I...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
2
by: Neil Chambers | last post by:
All, I have a class describing various actions to take against a LINQ to SQL datasource. What are the pros/cons of instantiating the LINQ object either in the root of the class (for lack of a...
5
by: CSharper | last post by:
I have a hashtable which has key and value. If the key is set to 'a' then I need to create a class1 and if it has key 'b' then I need to create class2 etc. (There is no name relation between the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.