我有一个类似的课程:
public class LogDataRow
{
public DateTime TheInterval { get; set; }
public ICollection<LogPreviousCurrent> PreviousCurrentItems { get; set; }
}
包含以下内容的集合:
public class LogPreviousCurrent : INotifyPropertyChanged
{
public string Name { get; set; }
private decimal? previous;
public decimal? Previous
{
get { return previous; }
set
{
previous = value;
PropertyNotification.Notify(this, PropertyChanged, PreviousProperty);
}
}
private const string PreviousProperty = "PreviousProperty";
private decimal? current;
public decimal? Current
{
get { return current; }
set
{
current = value;
PropertyNotification.Notify(this, PropertyChanged, CurrentProperty);
}
}
public const string CurrentProperty = "CurrentProperty";
}
我使用for循环创建一个LogDataRow集合,每个集合包含10个LogPreviousCurrent项目。每个LogPreviousCurrent都有一个Name属性。
这是一个for循环示例:
var logDataRows = new List<LogDataRow>();
// loop through for each interval (always 24)
for (int i = 1; i <= 24; i++)
{
// create a new logdatarow and intialise with the interval
var dataRow = new logDataRow
{
Interval = new Interval(customDateObject.TheDate, i)
};
// loop thorugh all the types (these equal columns in the destination grid)
foreach (var type in customTypes)
{
// initialise a new preiovuscurrent object
var previousCurrent = new LogPreviousCurrent
{
Name = type.Value.Name
};
DetailObject latestDetails = null;
DetailObject previousDetails = null;
if (latest.Details.ContainsKey(type.Key))
{
latestDetails = latest.Details[type.Key];
}
if (previous.Details.ContainsKey(type.Key))
{
previousDetails = previous.Details[type.Key];
}
// get the latest/previous interval value for each details object
var latestIntervalValue = latestDetails == null ? null : latestDetails.Details[i];
var previousIntervalValue = previousDetails == null ? null : previousDetails.Details[i];
// if there is a difference in the data
if (latestIntervalValue != previousIntervalValue)
{
previousCurrent.Previous = previousIntervalValue;
previousCurrent.Current = latestIntervalValue;
}
else
{
// no difference so set both to null
previousCurrent.Previous = null;
previousCurrent.Current = null;
}
dataRow.PreviousCurrentItems.Add(previousCurrent);
}
// only add rows that contain differences
if (dataRow.PreviousCurrentItems.Any(a => a.Previous != a.Current))
{
logDataRows.Add(dataRow);
}
}
在这个循环结束时,我得到一个只包含行(dataRow)的集合,其中包含LogPreviousCurrent差异。
现在我想做的是对于列来说是一样的。即,如果特定Name属性的所有LogPreviousCurrent都不包含任何差异,则logDataRows集合中不应存在这些LogPreviousCurrent对象。
所以基本上最后我想返回一个只包含LogPreviousCurrent项目的colllection,它们的Previous和Current属性之间存在差异。
我尝试过使用Linq,但迄今没有成功。
我希望这有道理吗?
答案 0 :(得分:0)
这就是我提出的:
// get all the PreviousCurrentItems across all logDataRows (flattened)
var allPreviousCurrentItems = logDataRows.SelectMany(p => p.PreviousCurrentItems).ToList();
// loop through all the types, again
foreach (var type in customTypes)
{
var name = type.Value.Name;
// get all the PreviousCurrentItems that match the Name
var matchedPreviousCurrentItems = allPreviousCurrentItems.Where(p => p.Name == name);
// if all the matched PreviousCurrentItems have pairs with no differences
if (matchedPreviousCurrentItems.All(p => p.Previous == p.Current))
{
// remove the matched items from the logDataRows collection
logDataRows.SelectMany(p => p.PreviousCurrentItems).ToList().RemoveAll(p => p.Name == name);
}
}
return logDataRows;