views:

36

answers:

1

Hi i have a model where i have to count the rows with conditions: uscitaservizio == false and accountid attribute (int) is in a string filtrodipe

es: filtrodipe = "2,4,5,6"

I' ve tried with this:

db.TDP_Missioni.Count( p => p.UscitaServizio == false && 
                    (objUser.FiltroDipe != null ? (p.AccountID.ToString() in objUser.FiltroDipe.Split(',')) : true))

But it's incorrect.

How can i do?

thanks

A: 

In a case like this I would normally pull apart my query logic to keep it easier to read (leave the in-line conditional out of the query itself). To simplify my sample I'm not safely parsing the accountIds, though I'd recommend doing so in your final code (use int.TryParse() instead).

var missions = db.TDP_Missioni.Where(p => !p.UscitaServizio);

if (objUser.FiltroDipe != null)
{
    var accountIds = objUser.FiltroDipe.Split(',').Select(a => int.Parse(a));
    missions = missions.Where(m => accountIds.Contains(m.AccountID));
}

var missionCount = missions.Count();

If you prefer to keep this all in a single query, the key portion to look at is accountIds.Contains(m.AccountID).
Changing (p.AccountID.ToString() in objUser.FiltroDipe.Split(',')) to objUser.FiltroDipe.Split(',').Contains(p.AccountID.ToString()) should do the trick, though I'd really recommend dealing with integers instead of strings.

Ryan Versaw