C# 如何将LINQ生成的对象上的方法作为委托传递?

C# 如何将LINQ生成的对象上的方法作为委托传递?,c#,linq,delegates,C#,Linq,Delegates,我有以下方法(其中SetTypes是枚举): public IEnumerable RetrieveCharts(IEnumerable过滤器,集合类型集合) { 开关(套) { case SetTypes.Set:返回GetChartsSet(过滤器); case SetTypes.Subset:返回getChartsubset(过滤器); //这里的其他案例。。。 违约: 抛出新的NotImplementedException(); } } 我在方法调用中复制代码。。。唯一改变的是Hash

我有以下方法(其中SetTypes是枚举):

public IEnumerable RetrieveCharts(IEnumerable过滤器,集合类型集合)
{
开关(套)
{
case SetTypes.Set:返回GetChartsSet(过滤器);
case SetTypes.Subset:返回getChartsubset(过滤器);
//这里的其他案例。。。
违约:
抛出新的NotImplementedException();
}
}
我在方法调用中复制代码。。。唯一改变的是HashSet实例上的方法调用:

private IEnumerable<Chart> GetChartsSet(IEnumerable<string> filters)
{
    using (var db = new ChartContext())
    {
        return db.Charts.Where(c => new HashSet<string>(c.ProductFilters.Select(f => f.Filter)).SetEquals(filters)).Select(c => c);
    }
}

private IEnumerable<Chart> GetChartsSubset(IEnumerable<string> filters)
{
    using (var db = new ChartContext())
    {
        return db.Charts.Where(c => new HashSet<string>(c.ProductFilters.Select(f => f.Filter)).IsProperSubsetOf(filters)).Select(c => c);
    }
}
// More duplicated methods follow...
private IEnumerable GetChartsSet(IEnumerable筛选器)
{
使用(var db=new ChartContext())
{
返回db.Charts.Where(c=>newhashset(c.ProductFilters.Select(f=>f.Filter)).SetEquals(filters)).Select(c=>c);
}
}
私有IEnumerable GetChartsubSet(IEnumerable筛选器)
{
使用(var db=new ChartContext())
{
返回db.Charts.Where(c=>newhashset(c.ProductFilters.Select(f=>f.Filter)).ispropertsubsetof(filters)).Select(c=>c);
}
}
//更多重复的方法如下。。。
与其让多个方法简单地在HashSet上调用不同的方法,我更喜欢一个将HashSet方法作为参数的合并方法——但我不确定如何继续。我该怎么做


如果您还告诉我如何摆脱切换语句,则可获得额外积分。:)

您可以制作一个通用的实现,该实现需要在集合中添加一个额外的组件:

private IEnumerable<Chart> GetWithPredicate(Predicate<HashSet<string>> pred) {
    using (var db = new ChartContext()) {
        return db.Charts
            .Where(c => pred(new HashSet<string>(c.ProductFilters.Select(f => f.Filter))))
            .Select(c => c);
    }
}
private IEnumerable GetWithPredicate(谓词pred){
使用(var db=new ChartContext()){
返回数据库图表
.Where(c=>pred(新HashSet(c.ProductFilters.Select(f=>f.Filter)))
.选择(c=>c);
}
}
然后以不同的方法重复使用:

private IEnumerable<Chart> GetChartsSet(IEnumerable<string> filters) {
    return GetWithPredicate(s => s.SetEquals(filters));
}

private IEnumerable<Chart> GetChartsSubset(IEnumerable<string> filters) {
    return GetWithPredicate(s => s.IsProperSubsetOf(filters));
}
private IEnumerable GetChartsSet(IEnumerable筛选器){
返回GetWithPredicate(s=>s.SetEquals(filters));
}
私有IEnumerable GetChartsubSet(IEnumerable筛选器){
返回GetWithPredicate(s=>s.ispropertsubsetof(filters));
}

谓词委托的使用很好,但是它在简洁的代码中有什么帮助呢?您不是还在分别调用GetChartsSet和GetChartsSubset吗。您仍然需要枚举类型。@MrinalKamboj此方法允许OP在多个方法之间共享
,这些方法仅在用于筛选集的特定谓词中有所不同。
private IEnumerable<Chart> GetChartsSet(IEnumerable<string> filters) {
    return GetWithPredicate(s => s.SetEquals(filters));
}

private IEnumerable<Chart> GetChartsSubset(IEnumerable<string> filters) {
    return GetWithPredicate(s => s.IsProperSubsetOf(filters));
}