문제

This is my first post on here so please be gentle!

I am new to c# and therefore very new to lambda. I am trying to construct a lambda statement and I am getting it wrong as it is not erroring but it is not returning any value.

What I'm trying to do is select some information from a table where the database select would be

SELECT slope
FROM   scaleTable
WHERE  percentage = aPercentagePassedIn

This should just return one record.

We are using Entity Framework.

This is what I have but as I said, I know it is wrong:

List<ScaleTable> maxSlope = ScaleTableList.Where(a => a.Slope.Equals(ScaleTableList.Where(b => b.Percentage.Equals(aPercentagePassedIn)))).ToList();

Thanks for any help.

Update 1

Many thanks for you help @m1Lb4nKs and @Habib. I have tried all options but for some reason it doesn't like the SingleOrDefault or the Where (error is table does not contain the definition for SingleOrDefault or Where) when I'm using it with the table name as:

var result = ScaleTable.SingleOrDefault(r => r.Percentage == aPercentagePassedIn);

or

var result = ScaleTable.Where(x => x.Percentage = aPercentagePassedIn).Select(y => y.slope).FirstOrDefault();    

Any ideas?

Update 2

Okay, thanks @Millie. I already had the using statement at the top so that's not the problem.

I changed my code to this but it errored with "Cannot implicitly convert type decimal to bool". Percentage is a decimal.

List<ScaleTable> maxSlope = ScaleTableList.Where(x => x.Percentage = aPercentagePassedIn).Select(y => y.slope).FirstOrDefault();

So I tried this but got an error that basically said it cannot convert type ScaleTable to System.Collections.Generic.List

List<ScaleTable> maxSlope = ScaleTableList.SingleOrDefault(r => r.Percentage == aPercentagePassedIn);

So, I'm still stumped and feeling "not very good at this"!!

도움이 되었습니까?

해결책 2

For those visiting this question, I managed to solve it myself using the appreciated help of those who responded. The code I ended up with is:

decimal maxSlope = ScaleTableList.Where(x => x.Percentage == aPercentagePassedIn).Select(y => y.Slope).FirstOrDefault(); 

다른 팁

You can simply do:

ScaleTableList.Where(x => x.percentage = aPercentagePassedIn).Select(y => y.slope).ToList()

If it should only return one record, then:

ScaleTableList.Where(x => x.percentage = aPercentagePassedIn).Select(y => y.slope).FirstOrDefault()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top