2017-11-13 1 views
0

Ich möchte zwei Listen mit Objekten aus zwei verschiedenen Klassen vergleichen. Dies sind die Klassen:Liste mit Ausnahme von zwei verschiedenen Klassen Instanzen

namespace AngularWebApplication.Models 
{ 
    public class AggregationLevelConfigurationPresentation 
    { 
     public byte AggregationLevelConfigurationId { get; set; } 
     public int ProductionOrderId { get; set; } 
     public string Name { get; set; } 
     [ ... ] 
    } 
} 

public class AggregationLevelConfiguration : IEquatable<AggregationLevelConfiguration> 
{ 
    public byte AggregationLevelConfigurationId { get; set; } 
    public int ProductionOrderId { get; set; } 
    public string Name { get; set; } 
    [ ... ] 
} 

Ich möchte die Elemente in presentations Liste erhalten, die nicht in currentLevels:

Liste Präsentationen; Liste currentLevels;

List<Models.AggregationLevelConfigurationPresentation> newLevels = 
    presentations 
     .Select(l => new { l.AggregationLevelConfigurationId, l.ProductionOrderId }) 
     .Except(currentLevels.Select(l => new { l.AggregationLevelConfigurationId, l.ProductionOrderId })) 
     .ToList(); 

Aber ich bekomme die folgende Fehlermeldung, wenn ich das tun Except:

Error CS0029 Cannot implicitly convert type 
'System.Collections.Generic.List<<anonymous type: byte AggregationLevelConfigurationId, int ProductionOrderId>>' to 
'System.Collections.Generic.List<AngularWebApplication.Models.AggregationLevelConfigurationPresentation>' 

denke ich, das Problem in den new { l.AggregationLevelConfigurationId, l.ProductionOrderId } ist, aber ich weiß nicht, wie die Except mit Liste von Objekten zu tun aus verschiedene Klassen.

Ich brauche die Objekte in presentations Liste, die sie nicht in currentLevelsAggregationLevelConfigurationId und ProductionOrderId als Primärschlüssel.

+2

könnte versuchen, den Titel neu formulieren? Ich konnte keinen Sinn daraus machen, bis ich die Frage gelesen hatte. – Fildor

+0

Sie wählen anonyme Typen aus. Warum erwarten Sie, dass 'ToList' eine' List 'erstellt? –

+0

@TimSchmelter Ich erwarte eine 'List '. – VansFannel

Antwort

4

Ihre Execept -Query wählt anonyme Typen aus, sodass ToList keine List<AggregationLevelConfigurationPresentation> erstellt. Sie müssen Instanzen dieser Klasse erstellen:

List<AggregationLevelConfigurationPresentation> newLevels = presentations 
     .Select(l => new { l.AggregationLevelConfigurationId, l.ProductionOrderId }) 
     .Except(currentLevels.Select(l => new { l.AggregationLevelConfigurationId, l.ProductionOrderId })) 
     .Select(x => new AggregationLevelConfigurationPresentation 
     { 
      AggregationLevelConfigurationId = x.AggregationLevelConfigurationId, 
      ProductionOrderId = x.ProductionOrderId 
     }) 
     .ToList(); 

Ich brauche die Objekte in Präsentationen Liste, dass sie nicht in currentLevels AggregationLevelConfigurationId und ProductionOrderId als Primärschlüssel verwenden.

Dann könnten Sie Join verwenden:

var except = presentations 
     .Select(l => new { l.AggregationLevelConfigurationId, l.ProductionOrderId }) 
     .Except(currentLevels.Select(l => new { l.AggregationLevelConfigurationId, l.ProductionOrderId })); 

var newLevels = from x in except 
       join p in presentations 
       on x equals new { p.AggregationLevelConfigurationId, p.ProductionOrderId } 
       select p; 
List<AggregationLevelConfigurationPresentation> newLevelList = newLevels.ToList(); 
+0

Ich bezweifle OP will das. Er möchte Objekte aus der ursprünglichen "Präsentationsliste", keine neuen Objekte (die sogar nur mit 2 Eigenschaften gefüllt sind). – Evk

+0

Aber das gibt eine Liste von Objekten mit nur einem Wert in 'AggregationLevelConfigurationId' und' ProductionOrderId' zurück und ich benötige die Objekte in der 'presentations' -Liste, die nicht in' currentLevels' sind und 'AggregationLevelConfigurationId' und' ProductionOrderId' als Primärschlüssel verwenden . – VansFannel

+0

@VansFannel: Ich habe meine Antwort bearbeitet –

0

Nach Evk Kommentar, das ist, wie ich dieses Problem gelöst haben:

List<Models.AggregationLevelConfigurationPresentation> newLevels = 
    presentations.Where(p => !currentLevels.Any(l => p.AggregationLevelConfigurationId == l.AggregationLevelConfigurationId && p.ProductionOrderId == l.ProductionOrderId)); 
Verwandte Themen