C# 除了两个不同类实例的列表外,不要列出

C# 除了两个不同类实例的列表外,不要列出,c#,C#,我想将两个列表与两个不同类的对象进行比较。这些是课程: namespace AngularWebApplication.Models { public class AggregationLevelConfigurationPresentation { public byte AggregationLevelConfigurationId { get; set; } public int ProductionOrderId { get; set; }

我想将两个列表与两个不同类的对象进行比较。这些是课程:

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; }
    [ ... ]
}
但是当我执行
时,除了
,我会得到以下错误:

Error   CS0029  Cannot implicitly convert type 
'System.Collections.Generic.List<<anonymous type: byte AggregationLevelConfigurationId, int ProductionOrderId>>' to 
'System.Collections.Generic.List<AngularWebApplication.Models.AggregationLevelConfigurationPresentation>'
错误CS0029无法隐式转换类型
“System.Collections.Generic.List”到
'System.Collections.Generic.List'
我认为问题出在
新的{l.AggregationLevelConfigurationId,l.productionordired}
中,但我不知道如何处理
,除了使用不同类的对象列表


我需要
演示文稿
列表中的对象,它们不在
当前级别
中,使用
AggregationLevelConfigurationId
ProductionOrderId
作为主键。
您的
Execept
查询选择匿名类型,因此
ToList
不会创建
列表
。您必须创建此类的实例:

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

您的
execep
-查询选择匿名类型,因此
ToList
不会创建
列表。您必须创建此类的实例:

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

以下是Evk的评论,我是如何解决这个问题的:

List<Models.AggregationLevelConfigurationPresentation> newLevels =
     presentations.Where(p => !currentLevels.Any(l => p.AggregationLevelConfigurationId == l.AggregationLevelConfigurationId && p.ProductionOrderId == l.ProductionOrderId));
列出新级别=
其中(p=>!currentLevels.Any(l=>p.AggregationLevelConfigurationId==l.AggregationLevelConfigurationId&&p.ProductionOrderId==l.ProductionOrderId));

在Evk的评论之后,我就是这样解决这个问题的:

List<Models.AggregationLevelConfigurationPresentation> newLevels =
     presentations.Where(p => !currentLevels.Any(l => p.AggregationLevelConfigurationId == l.AggregationLevelConfigurationId && p.ProductionOrderId == l.ProductionOrderId));
列出新级别=
其中(p=>!currentLevels.Any(l=>p.AggregationLevelConfigurationId==l.AggregationLevelConfigurationId&&p.ProductionOrderId==l.ProductionOrderId));

能否尝试重新表述标题?在阅读问题之前,我无法理解它。您选择的是匿名类型,那么为什么希望
ToList
创建一个
List
?@TimSchmelter我希望有一个
List
。在这种情况下,我建议不要使用
,除非使用
。相反,您可以执行类似于
presentations.Where(p=>!currentLevel.Any(l=>p.AggregationLevelConfigurationId==l.AggregationLevelConfigurationId))
@CodeCaster的操作,在所有版本中,问题都有“我想获取presentations列表中不在currentLevel中的元素”。编译器错误只是OPs尝试执行此操作的副产品。是否可以尝试重新表述标题?在阅读问题之前,我无法理解它。您选择的是匿名类型,那么为什么希望
ToList
创建一个
List
?@TimSchmelter我希望有一个
List
。在这种情况下,我建议不要使用
,除非使用
。相反,您可以执行类似于
presentations.Where(p=>!currentLevel.Any(l=>p.AggregationLevelConfigurationId==l.AggregationLevelConfigurationId))
@CodeCaster的操作,在所有版本中,问题都有“我想获取presentations列表中不在currentLevel中的元素”。编译器错误只是OPs尝试这样做的副产品。我怀疑OPs想要这样做。他想要的是原始的
演示文稿
列表中的项目,而不是新项目(即使只有两个属性已填充)。但这将返回一个对象列表,其中只有
AggregationLevelConfigurationId
ProductionOrderId
中的值,我需要
presentations
列表中的对象,它们不在
currentLevel
中,使用
AggregationLevelConfigurationId
ProductionOrderId
作为主键。I我不想这样。他想要的是原始的
演示文稿
列表中的项目,而不是新项目(即使只有两个属性已填充)。但这将返回一个对象列表,其中只有
AggregationLevelConfigurationId
ProductionOrderId
中的值,我需要
presentations
列表中的对象,它们不在
currentLevel
中,使用
AggregationLevelConfigurationId
ProductionOrderId
作为主键。