473,385 Members | 1,324 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,385 software developers and data experts.

Calculation using LINQ

Hello,

I have a List<MyObjectwhere MyObject has two properties: N (int) and
Weight (int).

In this list only the N is defined for each tag.

I want to calculate the Weight for each MyObject in the list, as
follows:

Weight of MyObject = N of Tag / Maximum N of all MyObject in list

How can I do this using a Linq expression?

Thanks,
Miguel
Sep 4 '08 #1
4 2405
Hi shapper,

It can be like this:

var secondList = from item in list select new MyObject
{
N = item.N,
Weight = item.N/ (from aggitem in list select item.N).Max() // or simple
list.Max(l =l.N)
};

Regards, Alex Meleta
mailto:am*****@gmail.com; blog:devkids.blogspot.com
Hello,

I have a List<MyObjectwhere MyObject has two properties: N (int) and
Weight (int).

In this list only the N is defined for each tag.

I want to calculate the Weight for each MyObject in the list, as
follows:

Weight of MyObject = N of Tag / Maximum N of all MyObject in list

How can I do this using a Linq expression?

Thanks,
Miguel

Sep 4 '08 #2
"Alex Meleta" <am*****@gmail.comwrote in message
news:df*************************@news.microsoft.co m...
It can be like this:

var secondList = from item in list select new MyObject
{
N = item.N,
Weight = item.N/ (from aggitem in list select item.N).Max() // or simple
list.Max(l =l.N) };
This obviously works, but it will recalculate list.Max for every item in the
list. Better to calculate it outside the LINQ expression and then reuse the
stored value inside.
Sep 4 '08 #3
"shapper" <md*****@gmail.comwrote in message
news:49**********************************@1g2000pr e.googlegroups.com...
Hello,

I have a List<MyObjectwhere MyObject has two properties: N (int) and
Weight (int).

In this list only the N is defined for each tag.

I want to calculate the Weight for each MyObject in the list, as
follows:

Weight of MyObject = N of Tag / Maximum N of all MyObject in list

How can I do this using a Linq expression?
You don't. LINQ takes one sequence as input, and produces another new
sequence at the output. From your description, you want to mutate the values
in-place, and within the same List - in which case foreach is the best tool
for the job here (though you could still use Enumerable.Max() to calculate
the maximum N first).
Sep 4 '08 #4
Hi Pavel,

Sure, mate. But we're just talking about the way, regardless of best one :)

Regards, Alex Meleta
mailto:am*****@gmail.com; blog:devkids.blogspot.com
"Alex Meleta" <am*****@gmail.comwrote in message
news:df*************************@news.microsoft.co m...
>It can be like this:

var secondList = from item in list select new MyObject
{
N = item.N,
Weight = item.N/ (from aggitem in list select item.N).Max() // or
simple
list.Max(l =l.N) };
This obviously works, but it will recalculate list.Max for every item
in the list. Better to calculate it outside the LINQ expression and
then reuse the stored value inside.

Sep 4 '08 #5

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

Similar topics

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...
8
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
5
by: =?Utf-8?B?U2llZ2ZyaWVkIEhlaW50emU=?= | last post by:
Can someone help me convert this to the latest C# syntax using predicate/delegate? You can see my attempt in the comments. Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
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...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
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...
6
by: tshad | last post by:
I was looking at a page that showed how to set up a custom event and it seems to work ok. But I am not sure how I would use it. How would I subscribe to it. There is actual action (such as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.