泛型和IList相关

时间:2012-06-18 18:07:40

标签: c# generics

我在下面有这个代码 - 一个将List作为输入参数之一的通用方法。在方法中,是一个IF / ELSEIF语句,它运行一个公共LINQ调用。 请帮助我,以便LINQ,呼叫可以是所有IF / ELSEIF的通用。

private Boolean filterList<T>(List<T> anyOutdoorSports, int numberOfPartcipants)
{
    if (anyOutdoorSports == null){ return false ;}
    Boolean _returnValue = false;

    if (anyOutdoorSports.GetType() == typeof(List<Swimming>))
    {
        List<Swimming> Swimming = anyOutdoorSports.Cast<Swimming>().ToList();

        if (Swimming.Count > 0)
        {
            int listCount = (from rca in Swimming
                             where (rca.RecordFields[numberOfPartcipants].ToString()).StartsWith("stamina")
                             select rca).Count();
            _returnValue = listCount > 0 ? true : false;
        }
    }
    else if (anyOutdoorSports.GetType() == typeof(List<Tennis>))
    {
        List<Tennis> Tennis = anyOutdoorSports.Cast<Tennis>().ToList();

        if (Tennis.Count > 0)
        {
            int listCount = (from rca in Tennis
                             where (rca.RecordFields[numberOfPartcipants].ToString()).StartsWith("stamina")
                             select rca).Count();
            _returnValue = listCount > 0 ? true : false;
        }
    }
    else if (anyOutdoorSports.GetType() == typeof(List<Soccer>))
    {
        List<Soccer> Soccer = anyOutdoorSports.Cast<Soccer>().ToList();

        if (Soccer.Count > 0)
        {
            int listCount = (from rca in Soccer
                             where (rca.RecordFields[numberOfPartcipants].ToString()).StartsWith("stamina")
                             select rca).Count();
            _returnValue = listCount > 0 ? true : false;
        }
    }

    return _returnValue;
}

感谢所有观看和帮助的人。

4 个答案:

答案 0 :(得分:2)

如果TennisSoccerSwimming的公共基类具有RecordFields属性,则这很简单:

private boolean FilterList<T>(IEnumerable<T> anyOutdoorSports, int numberOfParticipants) where T : OutdoorSport
{
    if(anyOutdoorSports == null) return false;
    return anyOutdoorSports.Any(s => s.RecordFields[numberOfParticipants].ToString().StartsWith("stamina"));
}

答案 1 :(得分:0)

由于您的所有类型似乎都有RecordFields属性,您可以添加一个接口并使用它而不是Generic。

除此之外,你必须求助于Func&lt;&gt;表达式。

答案 2 :(得分:0)

假设所有类都继承自包含RecordFields的同一基类,您可以在该方法上创建一个通用约束并完全删除ifs

private Boolean filterList<T>(List<T> anyOutdoorSports, int numberOfPartcipants)
  where T : OutdoorSport
{
    if (anyOutdoorSports == null){ return false ;}
    Boolean _returnValue = false;

    if (anyOutdoorSports.Count > 0)
    {
        int listCount = (from rca in anyOutdoorSports
                         where (rca.RecordFields[numberOfPartcipants].ToString()).StartsWith("stamina")
                         select rca).Count();
        _returnValue = listCount > 0;
    }

    return _returnValue;
}

答案 3 :(得分:0)

我看到两个选项:

1)使用Where子句过滤每个类型(将采用三行)

List<Soccer> Soccer = anyOutdoorSports.Where(x => x.GetType() == typeof(List<Swimming>)).ToList()

做那种运动类型。

2)使用GroupBy LINQ扩展按GetType()

对它们进行分组
var groups = anyOutdoorSports.GroupBy(x => x.GetType())

这将使一个对象成为键的字典,这些键是作为体育的类型和值

然后你终于可以看到哪些有任何值,然后确定从那里返回什么。