vendredi 1 juillet 2016

Writing if then else in Linq to Entities query

I wrote following code to count refers to a Document to:

var currentStationId = GetCurrentStation();
var result = dbContext
            .Documents
            .Select(x=> 
                    new MyDTO{
                            ...,
                            ReferCounts = x.DocumentStationHistories
                                           .Count(t => t.ToStationId == currentStationId))
                             }

Now I want to change mechanism of calculating ReferCounts:

if(currentStationId == 1) ReferCounts = x.DocumentStationHistories
                                         .Count(t => t.ToStationId == currentStationId &&
                                                     t.FromStationId != t.ToStationId))
else
    ReferCounts = x.DocumentStationHistories
                   .Count(t => t.ToStationId == currentStationId))

How can I use this code in my linq to entities query?

Aucun commentaire:

Enregistrer un commentaire