This seems really simple, but I can't figure out what I am missing. I am trying to cast an item from a list to a decimal. The error I am getting is Can not implicitly convert type to decimal. Thanks in advance.
public class specialTeam
{
public decimal Scores {get;set;}
public specialTeam(string awayTeam)
{
Scores = bBall.getStatList().Where(t => t.Teams == awayTeam).Select(s => s.score);
}
}
Loading

VulpesRank #55 · 29.2K reputationPosted Mar 1, 2012, 12:26 AM EST
If there's only one result then you can use the First() extension method.
P.S.
Did you remember to change the type of the Scores property to decimal[] ?
Sam HobbsRank #107 · 20.5K reputationPosted Feb 29, 2012, 10:53 PM EST
That is a problem with powerful tools. Your one line is actually doing multiple things. That is good when it works but it is difficult to diagnose when it does not work.
Are you assuming that the Select method returns an item? Are you sure it does?
Rich83 reputationPosted Feb 29, 2012, 11:10 PM EST
VulpesRank #55 · 29.2K reputationPosted Feb 29, 2012, 11:43 PM EST
Rich83 reputationPosted Feb 29, 2012, 11:55 PM EST
public specialTeam(string awayTeam)
{
Scores = bBall.getStatList().Where(t => t.Teams == awayTeam).Select(s => s.score.score);
}
But now the error is Can not implicitly convert IEnumerable
VulpesRank #55 · 29.2K reputationPosted Mar 1, 2012, 12:02 AM EST
Rich83 reputationPosted Mar 1, 2012, 12:21 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Mar 1, 2012, 12:42 AM EST
Rich83 reputationPosted Mar 1, 2012, 12:44 AM EST
public List
Scores = bBall.getStatList().Where(t => t.Teams == awayTeam).Select(s => s.score.score).ToList();
or
public decimal Scores {get;set;}
Is there an advantage by one over the other?
Rich83 reputationPosted Mar 1, 2012, 12:47 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 1, 2012, 12:49 AM EST
If there may be one or no results, then you can use FirstOrDefault() rather than First().
If there were no results, this would then set Scores to 0m which is the decimal type's default value.
Rich83 reputationPosted Mar 1, 2012, 12:54 AM EST