Reputation: 1160
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);
Upvotes: 4
Views: 21525
Reputation: 23732
Building up on Tim and Dipin, the First()
method throws unfortunately an InvalidOperationException when the query does not yield any results.
Either you surround it with a try/catch block
try{
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;
}
catch(InvalidOperationException ex){ // tell no Result!}
or you could use FirstOrDefault()
which will return a null-reference when the query yields no match.
//... Example from above
}).FirstOrDefault();
// check for null-reference
if(latest != null)
var decalExpireDate = latest.DecalExpireDate;
var decalExpireMonth = latest.DecalExpireMonth;
var decalExpireYear = latest.DecalExpireYear;
}
A third possibility could be to take the first Element using Take(1)
//... Example from above
}).Take(1).ToList(); // returns an empty list if no match
// check whether something was returned
if(latest.Any())
var decalExpireDate = latest[0].DecalExpireDate;
var decalExpireMonth = latest[0].DecalExpireMonth;
var decalExpireYear = latest[0].DecalExpireYear;
}
Upvotes: 0
Reputation: 1
var q = from r in tab1.dictenaries
select new {
r.ENGLISH,
r.FRENCH
};
List<SomeClass> sml = new List<SomeClass>();
int starting = 10;
int ending = 20;
int cntr = 0;
foreach( var verObj in q)
{
if (cntr >= starting && cntr <= ending)
{
SomeClass sm1 = new SomeClass();
sm1.ENGLISH = verObj.ENGLISH.ToString();
sm1.FRENCH = verObj.FRENCH.ToString();
sml.Add(sm1);
}
else if(cntr >ending)
{
break;
}
cntr = cntr + 1;
}
Upvotes: 0
Reputation: 123
Making the same assumptions as Tim, you could also go with
var result = db.CT_Decals.OrderByDescending(d => d.DecalExpireDate).First(d => d.TankID == decal.TankID && d.DecalStatus == "Approved");
var expireDate = result.DecalExpireDate;
var expireMonth = result.DecalExpireMonth;
var expireYear = result.DecalExpireYear;
You will have the desired object of type 'CT_Decals' in 'result' variable so access any property of it
Upvotes: 0
Reputation: 460118
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;
Upvotes: 11