Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
SolvedPrivate

# get the first Item in a list linq

Posted on 2014-04-22
Medium Priority
78 Views
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;

}
``````
0
Question by:r3nder
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3

LVL 75

Expert Comment

ID: 40016122
What is the type of gammaList?
0

LVL 6

Author Comment

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

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);
``````
0

LVL 6

Author Comment

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

käµfm³d   👽 earned 2000 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)
``````

You need to tweak your Where to accommodate such.
0

LVL 6

Author Comment

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

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

LVL 6

Author Closing Comment

ID: 40016287
Thanks Kaufmed
0

## Featured Post

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …
###### Suggested Courses
Course of the Month9 days, 23 hours left to enroll