SolvedPrivate

get the first Item in a list linq

Posted on 2014-04-22
8
72 Views
Last Modified: 2016-02-16
I am trying to get the first Item in a list and it keeps throwing an error on .Take(1) how can I get this to select the first column (thedepth) of 2 columns  in a list called gammalist
 int t2 = Convert.ToInt32(total);
                    for (int i = 0; t2 >= 100; i++)
                    {
                        int depthToRemove = Convert.ToInt32(Program.myMainControl.dtoc.gammaList.Where(g => g.theDepth).Take(1));
                        Program.myMainControl.dtoc.gammaList.RemoveAt(0);
                        gammaseries.DataPoints.RemoveAt(0);
                        dtg.Rows[0].Delete();
                        t2 = depthToRemove - t2;
                        
                    }

Open in new window

0
Comment
Question by:r3nder
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40016122
What is the type of gammaList?
0
 
LVL 6

Author Comment

by:r3nder
ID: 40016132
public struct gammaPlots
        {
            public float thedepth { get; set; }
            public float thegamma { get; set; }
        }
public List<gammaPlots> gammaList = new List<gammaPlots>();
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40016144
Your problem is you are passing an IEnumerable<gammaPlots> to Convert.ToInt32. I assume you meant to send the first theDepth value. I think you should be using First instead:

int depthToRemove = Convert.ToInt32(Program.myMainControl.dtoc.gammaList.Where(g => g.theDepth).First().theDepth);

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 6

Author Comment

by:r3nder
ID: 40016166
error on g.thedepth
Cannot convert lambda expression to delegate type 'System.Func<SimplyMWDClientGen2.DTO.DTOClass.gammaPlots,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type      
Connot implicitly convert type float to bool
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40016169
Ah, I didn't notice this before, but your Where clause is incomplete. The thing you pass to Where should yield a boolean result.

e.g.

Where(g => g.theDepth > 3.00)

Open in new window


You need to tweak your Where to accommodate such.
0
 
LVL 6

Author Comment

by:r3nder
ID: 40016277
var depthToRemove = Program.myMainControl.dtoc.gammaList.Where(g => g.thedepth >= 0.00).First().thedepth;
this gives me almost all the depths added together - I just want the first depth - what am I doing wrong - I don't see it
0
 
LVL 6

Author Comment

by:r3nder
ID: 40016285
Wait that is getting the depth - I need to do some math now
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40016287
Thanks Kaufmed
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question