String.Format in linq request

I'm stuck in someone else's problem. I have a class CashGameGeneralViewModelthat looks like

public class CashGameGeneralViewModel
{
    public string Limit { get; set; }
    public int HandsPlayed { get; set; }

    public float AmountWon { get; set; }
}

Here is a method that should return all the hands a particular player plays:

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

Now the problem is

Limit = //"some text",
String.Format("{0:0.00}", hh.SBlindAmount) + "/" +
String.Format("{0:0.00}", hh.BBlindAmount)

part. hh.SBlindAmountand hh.BBlindAmountare float values. I wanted to use String.Formatit because it was 0.10shortened to 0.1, and in the lowercase format I got it as if I wanted to. But I get an exception that 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 delete string.format and put in some kind of “normal” string, everything works fine ... Does anyone know why?

+3
source share
4 answers

, , , , , . , ViewModel, Limit, :

public string Limit { get { return string.Format("{0:0.00}/{1:0.00}", SmallBlind, BigBlind); } }

Edit:

, - . , ViewModel , , BigBlind/SmallBlind.

+1

, , ( float ), .ToString(), .

- SmallBlind.ToString("{0:0.00}")

, , , , :

Limit = string.Format("{0} / {1}",
           SmallBlind.ToString("{0:0.00}"),
           BigBlind.ToString("{0:0.00}")),
//Rest of statement here...

, ( ), :

Limit = GetLimit(SmallBlind, BigBlind),
//Rest of Statement Here

Get Limit string.Format:

private string GetLimit(double smallBlind, double bigBlind)
{
    return string.Format("{0} / {1}",
           smallBlind.ToString("{0:0.00}"),
           bigBlind.ToString("{0:0.00}"));
}

, , , Linq, .

, , , CashGameGeneralViewModel - Blinds. , ( ) , Getit getter .

, , , , , .

+2

, , SQL. , , . , , .

+1

:

CLR TSQL. , .ToString(string s),.ToShort... .. , , . (, , , / ..). T-SQL , , , .net, SQL RDBMSes. , .net DateTime.ToShortDateString TSQL SQL, , , , , , , .net . ( ).

, , , .net, DateTime.ToShortDateString DateTime.ToLongDateString / :

en-us (US English):
11/15/2010
Monday, November 15, 2010

sv-se (Swedish):
2010-11-15
den 15 november 2010

zh-cn (Chinese):
2010/11/15
2010年11月15日

ar-sa (Arabic / Saudi Arabia):
09/12/31
09/ذو الحجة/1431

th-th (Thai):
15/11/2553
15 พฤศจิกายน 2553

, , - (15 2010 .) /. T-SQL- , . , , ...:)

, : , .net, T-SQL .net-. , , , L2E .net, L2O (linq-to-objects), , .

:

- L2E, L2E, L2O, , .net-...

//Linq-to-entities query to get the fullname, categoryname, and date
var query = from c in db.Contacts select new { c.FullName, c.Category.CategoryName, c.DateCreated };

//Linq-to-objects query (.AsEnumerable will separate the L2E from L2O part) that call date formatting methods and other stuff that isn't supported by L2E
var r = from c in query.AsEnumerable() select new { c.FullName, c.CategoryName, ShortDate = c.DateCreated.ToString("D") };

( msdn)

+1

All Articles