Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq how to get values from var

Tags:

linq

c#-4.0

How do I get the individual values from the following LINQ query? (I want to get the value for DecalExpireDate, DecalExpireMonth, and DecalExpireYear.)

var previousExpirationDate = (from d in db.CT_Decals
                              where d.TankID == decal.TankID
                              && d.DecalStatus == "Approved"
                              select new
                              {
                                  d.DecalExpireDate,
                                  d.DecalExpireMonth,
                                  d.DecalExpireYear
                              }).Max(d => d.DecalExpireDate);
like image 486
user1202606 Avatar asked Dec 21 '22 19:12

user1202606


1 Answers

I assume you want to get the DecalExpireDate, DecalExpireMonth, and DecalExpireYear from the element with the highest (last) DecalExpireDate. Then you could order by this date:

var latest = (from d in db.CT_Decals
             where d.TankID == decal.TankID && d.DecalStatus == "Approved"
             orderby d.DecalExpireDate descending
             select new
             {
                 d.DecalExpireDate,
                 d.DecalExpireMonth,
                 d.DecalExpireYear
              }).First();
var decalExpireDate  = latest.DecalExpireDate;
var decalExpireMonth = latest.DecalExpireMonth;
var decalExpireYear  = latest.DecalExpireYear;
like image 72
Tim Schmelter Avatar answered May 26 '23 10:05

Tim Schmelter