1

我正在尝试将 2 个类合并到日期属性上的第三个类中,以绑定到我已经设置的图形。

public class Last30DaysHours
{
    public DateTime Date { get; set; }
    public float Hours { get; set; }
    public float LostHours { get; set; }
}

public class MachineHours
{
    public DateTime Date { get; set; }
    public float Hours { get; set; }
}

进入

public class GraphLast30Days
{
    public DateTime Date { get; set; }
    public float Hours { get; set; }
    public float LostHours { get; set; }
    public float SelectedMachine { get; set; }
}

到目前为止,我有这个几乎可以编译的 linq 语句。当前语句的错误是“当前上下文中不存在‘x’”。

我知道这意味着什么,但我不知道如何在语句中使其可访问。

IEnumerable<GraphLast30Days> last30DaysMachineHoursSelect = _last30DaysMachineHours
            .Select(p => (_last30Days
                            .Where(x => x.Date == p.Date)  <= 
                            (new GraphLast30Days { 
                                                    Date = x.Date, 
                                                    Hours = x.Hours, 
                                                    LostHours = x.LostHours, 
                                                    SelectedMachine = p.Hours 
                                                  })));             

我的问题是如何使语句的后半部分可以访问 x 或者什么是更好的语句来实现相同的结果?

谢谢您的帮助。

4

1 回答 1

5

你不会使用 join 按日期加入你的收藏(不确定我得到了正确的属性,但你明白了):

IEnumerable<GraphLast30Days> last30DaysMachineHoursSelect = 
        from machineHours in _last30DaysMachineHours
        join last30 in _last30Days on machineHours.Date equals last30.Date
        select
        new GraphLast30Days { 
            Date = machineHours.Date, 
            Hours = machineHours.Hours, 
            LostHours = last30.LostHours, 
            SelectedMachine = machineHours.Hours
            };

或使用替代语法:

    var result = _last30DaysMachineHours.Join(_last30Days, graph => graph.Date, last30 => last30.Date, 
                                             (graph, last30) => new GraphLast30Days
                                                                {
                                                                        Date            = graph.Date,
                                                                        Hours           = graph.Hours,
                                                                        LostHours       = last30.LostHours,
                                                                        SelectedMachine = graph.Hours
                                                                });

如果您不想过滤掉缺失值,则需要进行左连接:

IEnumerable<GraphLast30Days> last30DaysMachineHoursSelect =
    from last30 in _last30Days
    from machineHours in _last30DaysMachineHours.Where (h => h.Date == last30.Date).DefaultIfEmpty()
    select
    new GraphLast30Days { 
        Date = last30.Date, 
        Hours = last30.Hours, 
        LostHours = last30.LostHours, 
        SelectedMachine = machineHours == null ? 0 : machineHours.Hours
        }
于 2012-12-19T22:37:56.943 回答