473,474 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 11542
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.