0

我有这样的课:

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);
            }
        }

在这个循环结束时,我最终得到一个集合,其中只有包含 LogPreviousCurrent 差异的行(dataRow's)。

现在我想要对列做同样的事情。即,如果特定 Name 属性的所有 LogPreviousCurrent 不包含差异,则这些 LogPreviousCurrent 对象不应存在于 logDataRows 集合中。

所以基本上最后我想返回一个只包含 LogPreviousCurrent 项目的集合,它们的 Previous 和 Current 属性之间存在差异。

我曾尝试使用 Linq 执行此操作,但迄今为止没有成功。

我希望这是有道理的?

4

1 回答 1

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;
于 2013-07-03T04:28:11.610 回答