Linq query String.Format

.net c#-4.0 entity-framework linq wpf


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

        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?

5/17/2011 6:46:21 PM

Accepted Answer

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); } }


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.

5/17/2011 6:59:20 PM

Popular Answer

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}",
//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}",

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow