C#如何使用linq实现继承?

C#如何使用linq实现继承?,c#,linq,inheritance,C#,Linq,Inheritance,我有两个基于同一类的列表。 我希望一个列表继承另一个列表的类属性的值 class feature { Guid id; string featureName; string featureType; ..... } List <feature> companyFeatures; List <feature> divisionFeatures; 请注意,Guid2现在的type属性值为text。 希望这能澄清问题。从技术上讲,这不是继承- 如果此

我有两个基于同一类的列表。 我希望一个列表继承另一个列表的类属性的值

class feature {
   Guid id;
   string featureName;
   string featureType;
   .....
}

List <feature> companyFeatures;
List <feature> divisionFeatures;
请注意,Guid2现在的type属性值为text。
希望这能澄清问题。

从技术上讲,这不是继承-

如果此处的目标是返回
companyFeatures
divisionFeatures
列表中所有功能的集合,则您可以执行以下操作:

IEnumerable<feature> totalFeatures = companyFeatures.Concat(divisionFeatures);
IEnumerable totalFeatures=companyFeatures.Concat(divisionFeatures);

该方法将返回一个
IEnumerable
,其中包含两个列表中的元素。

从技术上讲,这不是继承-

如果此处的目标是返回
companyFeatures
divisionFeatures
列表中所有功能的集合,则您可以执行以下操作:

IEnumerable<feature> totalFeatures = companyFeatures.Concat(divisionFeatures);
IEnumerable totalFeatures=companyFeatures.Concat(divisionFeatures);

该方法将返回一个
IEnumerable
,其中包含两个列表中的元素。

如果我误解了,请纠正我。您有一个
公司
类和一个
部门
类,它们都有一个
列表
字段?类似于

public class Company {
    // ...
    public List<Feature> CompanyFeatures;
    // ...
}
public class Division {
    public Company Company;  // The company to which the division belongs
    // ...
    public List<Feature> DivisionFeatures;
    // ...
}
上市公司{
// ...
上市公司特征;
// ...
}
公共课组{
上市公司;//该部门所属的公司
// ...
公共列表功能;
// ...
}
如果是这种情况,那么我建议将特性列表转换为只读属性,并让它自动处理“继承”。例如

public class Division {
    public Company Company;  // The company to which the division belongs
    // ...
    private List<Feature> _divisionFeatures;  // private now
    public IEnumerable<Feature> DivisionFeatures {
        get {
            // Take all the features for the divison...
            return _divisionFeatures.Concat(
                // ... and add all the company features except for those that
                // have the same name as one of the division features.
                Company.CompanyFeatures.Where(cf => !_divisionFeatures
                    .Any(df => df.Name == cf.Name))
            );
        }
    }
    // ...
}
公共类划分{
上市公司;//该部门所属的公司
// ...
私有列表_divisionFeatures;//立即私有
公共数字特征{
得到{
//以除法的所有特征为例。。。
return\u.Concat(
//…并添加除以下功能外的所有公司功能
//与某个分区功能同名。
Company.CompanyFeatures.Where(cf=>!\u分区功能
.Any(df=>df.Name==cf.Name))
);
}
}
// ...
}

或者,当然您仍然可以将
列表
公开(以便您仍然可以从外部对其进行操作),并调用类似
DivisionFeaturesWithInheritance
的属性,如果我误解了,请纠正我。您有一个
公司
类和一个
部门
类,它们都有一个
列表
字段?类似于

public class Company {
    // ...
    public List<Feature> CompanyFeatures;
    // ...
}
public class Division {
    public Company Company;  // The company to which the division belongs
    // ...
    public List<Feature> DivisionFeatures;
    // ...
}
上市公司{
// ...
上市公司特征;
// ...
}
公共课组{
上市公司;//该部门所属的公司
// ...
公共列表功能;
// ...
}
如果是这种情况,那么我建议将特性列表转换为只读属性,并让它自动处理“继承”。例如

public class Division {
    public Company Company;  // The company to which the division belongs
    // ...
    private List<Feature> _divisionFeatures;  // private now
    public IEnumerable<Feature> DivisionFeatures {
        get {
            // Take all the features for the divison...
            return _divisionFeatures.Concat(
                // ... and add all the company features except for those that
                // have the same name as one of the division features.
                Company.CompanyFeatures.Where(cf => !_divisionFeatures
                    .Any(df => df.Name == cf.Name))
            );
        }
    }
    // ...
}
公共类划分{
上市公司;//该部门所属的公司
// ...
私有列表_divisionFeatures;//立即私有
公共数字特征{
得到{
//以除法的所有特征为例。。。
return\u.Concat(
//…并添加除以下功能外的所有公司功能
//与某个分区功能同名。
Company.CompanyFeatures.Where(cf=>!\u分区功能
.Any(df=>df.Name==cf.Name))
);
}
}
// ...
}

或者,当然,您仍然可以将
列表
公开(以便您仍然可以从外部对其进行操作),并调用类似
DivisionFeaturesWithInheritance

的属性。您可以发布LINQ查询和数据架构吗?您可以发布LINQ查询和数据架构吗?谢谢,我理解你的答案,但我想我的问题还不够清楚。我将添加一个示例,因为我的目标是部门属性将覆盖公司属性。@Timwi:谢谢,我刚刚编辑了示例。我仍在努力理解你答案的细节(如下)。谢谢,我理解你的答案,但我想我的问题还不够清楚。我将添加一个示例,因为我的目标是部门属性将覆盖公司属性。@Timwi:谢谢,我刚刚编辑了示例。我仍在努力理解你的答案(如下)的细节。就我理解你的代码而言,你在公司级的功能列表中添加了任何部门级的“新”功能。代码不错,但是。我试图覆盖两个列表中存在的功能的特定字段。(我希望我上面的例子能够澄清)无论如何,谢谢你的帮助。@Julian:你误解了代码。它接受了部门级别的所有功能,只添加了公司级别中尚未包含在部门级别的功能。请接受我的道歉,你是对的。你的解决方案非常适合我的问题。谢谢。我可以点击答案复选标记,但我是新来的系统,还不能提高有用的指标。没关系,不用担心☺据我所知,您的代码将在部门级别的“新”功能添加到公司级别的功能列表中。代码不错,但是。我试图覆盖两个列表中存在的功能的特定字段。(我希望我上面的例子能够澄清)无论如何,谢谢你的帮助。@Julian:你误解了代码。它接受了部门级别的所有功能,只添加了公司级别中尚未包含在部门级别的功能。请接受我的道歉,你是对的。你的解决方案非常适合我的问题。谢谢。我可以点击答案复选标记,但我是新来的