简体   繁体   中英

Linq merging two classes into a third?

I am trying to merge 2 classes into a third on the date properties, for binding to a graph I have already setup.

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

into

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

So far I have this linq statement which almost compiles. The error with the current statment is "'x' does not exist in the current context".

I know what this means but I don't know how to make it accessable in the statement.

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

My question is how do I make x accessable by the second half of the statement or what is a better statement to achieve the same results?

Thanks for the help.

You'd wont to use join to join your collections by date (not sure I got the correct proeprties, but you got the idea):

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

Or with alternative syntax:

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

In case you don't wont to filter out missing values you'll need to do left join:

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
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM