I have a conditional as such:
List<HWSRunSession> session = new List<HWSRunSession>();
foreach (var item in fileInfo)
{
if(_db.HWSRunSessions.Where((x) => x.TransferredZipName == item.Name
&& DateTime.Now.Subtract(x.AddedDate).TotalDays >= _ExpirationDays) == null) {
bla bla...
}
}
But I want to save the list I retrieve in the conditional to my variable session
using the "out" key word. kind of like:
List<HWSRunSession> session = new List<HWSRunSession>();
foreach (var item in fileInfo)
{
if(_db.HWSRunSessions.Where((x) => x.TransferredZipName == item.Name
&& DateTime.Now.Subtract(x.AddedDate).TotalDays >= _ExpirationDays), out session == null) {
}
}
Is this possible and if so how?
The most readable and maintainable solution is to store the result in a variable, then test this variable in the condition:
var session = _db.HWSRunSessions
.Where((x) => x.TransferredZipName == item.Name
&& DateTime.Now.Subtract(x.AddedDate).TotalDays >= _ExpirationDays)
.FirstOrDefault();
if(session == null)
{
// session is null
}
else
{
// do something with session
}
If you absolutely must assign a variable within an expression, it is possible. But it makes the code more difficult to read and easier to miss bugs:
Session session = null;
if ((session = _db.HWSRunSessions
.Where((x) => x.TransferredZipName == item.Name
&& DateTime.Now.Subtract(x.AddedDate).TotalDays >= _ExpirationDays)
.FirstOrDefault()) == null)
{
// session is null
}
else
{
// do something with session
}