My Log class has 2 properties.
List<LogDetails>
string Title
And my LogDetails class has 2 properties:
string Message
LogStatus LogStatus //enum
During this objects life time, it receives many messages which are stored for logging at a later stage. The LogStatus Enum has a few states, but simply it is either “in progress”, “file not found error”, “success” or “failed”.
I am trying to use Linq to match only those which failed, but I can’t build the new type.
List<Log> filteredLogs = (from a in logs
from b in a.LogDetailsList
where b.Status == LogDetails.LogStatus.Failed
select new Log()
{
Title = a.Title,
LogDetailsList = a.LogDetailsList
}).Distinct().ToList();
My filteredLogs returns to me the full list as if the where clause hadn’t worked.
I tried
List<Log> filteredLogs = (from a in logs
from b in a.LogDetailsList
where b.Status == LogDetails.LogStatus.Failed
select new Log()
{
Title = a.Title,
LogDetailsList = new LogDetails() { Message = b.Message, Status = b.Status }
}).Distinct().ToList();
This doesn’t work either as it appears I can’t call the ToList() method from within the Linq query.
EDIT
Does any one have any suggestions on how to overcome this issue? My goal is to have a List which only lists those which failed.
Use
Enumerable.Anyon your “nested”LogDetailslist.This returns all logs where at least one log-detail has
LogStatus.Failed.Here’s a Demo.