I am stuck on a wierd problem. I have a CashGameGeneralViewModel
class wich looks like this
public class CashGameGeneralViewModel
{
public string Limit { get; set; }
public int HandsPlayed { get; set; }
public float AmountWon { get; set; }
}
Here is the method that should return all hands played by a certain player:
public List<CashGameGeneralViewModel> GetAllHands(string playerToFind)
{
HoldemHandContext db = new HoldemHandContext();
int playerId = GetPlayerId(playerToFind);
var holdemHandResult = (from phh in db.PlayersInHoldemHands
from hh in db.HoldemHands
where hh.Id == phh.HandPlayed && phh.PlayerId == playerId
select new CashGameGeneralViewModel()
{
Limit = //"some text",
String.Format("{0:0.00}", hh.SBlindAmount) + "/" +
String.Format("{0:0.00}", hh.BBlindAmount),
HandsPlayed = db.HoldemHands.Distinct().Count(),
AmountWon = 0
}
).ToList();
return holdemHandResult;
}
public int GetPlayerId(string playerToFind)
{
HoldemHandContext db = new HoldemHandContext();
int playerId = (from p in db.Players
where p.ScreenName == playerToFind
select p.Id).FirstOrDefault();
return playerId;
}
The problem now is the
Limit = //"some text",
String.Format("{0:0.00}", hh.SBlindAmount) + "/" +
String.Format("{0:0.00}", hh.BBlindAmount)
part. hh.SBlindAmount
and hh.BBlindAmount
are float values. I wanted to use String.Format
because 0.10
is shortened to 0.1
and with the string format I got it like i want it. But I am getting an exception wich says 'The invocation of the constructor on type 'PokerRecord.View.CashGameGeneralUC' that matches the specified binding constraints threw an exception.' Line number '60' and line position '18'.
. When I remove the string.format and put in some "regular" string everything works fine... Anyone knows why?
Another answer I just thought of, and the way I'd probably prefer to do it. I'd say just store those original values in the ViewModel and then change your Limit property to just create the string based on those values:
public string Limit { get { return string.Format("{0:0.00}/{1:0.00}", SmallBlind, BigBlind); } }
Edit:
I'll add my reasoning for preferring it this way - it's non-destructive. But that may be overkill or completely unnecessary if your ViewModel isn't going to change much or you know you'll never need the BigBlind/SmallBlind properties in the future.
I think for what you're trying to do (format a specific float into a string) you want the overload of .ToString()
which allows you to provide a format provider.
Something like SmallBlind.ToString("{0:0.00}")
What you're probably looking for might best be represented:
Limit = string.Format("{0} / {1}",
SmallBlind.ToString("{0:0.00}"),
BigBlind.ToString("{0:0.00}")),
//Rest of statement here...
Based on the error you're getting (I got a similar one in a problem yesterday) here's my solution:
Limit = GetLimit(SmallBlind, BigBlind),
//Rest of Statement Here
Then define Get Limit with the string.Format:
private string GetLimit(double smallBlind, double bigBlind)
{
return string.Format("{0} / {1}",
smallBlind.ToString("{0:0.00}"),
bigBlind.ToString("{0:0.00}"));
}
I'll leave it to better experts than I on WHY that's causing a failure in Linq, but that should get you around it.
This, of course, assumes that your CashGameGeneralViewModel should not be aware of the Blinds for some reason. If it can be, the solution (already mentioned in another answer) is to have the Limit getter return the pre-formated string.
There may be a better way to do what I'm doing, but, running into the same problem you've got, that's how I solved it.