Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Linq blues
Message
Information générale
Forum:
C#
Catégorie:
LINQ
Titre:
Divers
Thread ID:
01662671
Message ID:
01662691
Vues:
44
>I have this linq statement:
>
>
>            q = context.TransactionDetails
>                .Where(t => t.Parcel.GoodsType.Description == "Rough")
>                .Where(x => x.TransactionHeader.FiscalYears_Id == _FiscalYear_ID)
>                .Where(t => t.TransactionHeader.TransactionType.Description == "Purchase")
>                .GroupBy(t => 1)
>                .Select(t => new { Weight = t.Sum(x => x.Weight), Amount = t.Sum(x => x.Amount) })
>                .ToList();
>
> and then I use the results in
>
>            wt.AddRow("Purchases", q[0].Weight, q[0].Amount);
>
>
>This breaks if there are no records selected, in which case I want to pass 0 for Weight and Amount to wt.AddRow.
>
>I'm looking for the proper way to do this, as I have 10's of these statements. The quick and dirty way would be wrapping them in try catch.
>
>Thanks for your help.


Simple:

>
             if (q==null || q.Count==0)
             {
                wt.AddRow("Purchases", 0, 0);
             }       
else {
>            wt.AddRow("Purchases",  q[0].Weight, q[0].Amount);
}
>
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform