Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以类似Rails范围的方式链接C#LINQ_C#_Linq - Fatal编程技术网

以类似Rails范围的方式链接C#LINQ

以类似Rails范围的方式链接C#LINQ,c#,linq,C#,Linq,我在下面的类中存储了一些静态数据。我还有一些方法可以使用LINQ过滤这些数据。像这样: static class Data { // ... static public Weapon[] weapons = new Weapon[] { // ... } static public GetUnlockedWeapons() { Data.weapons.Where( a => a.unlocked ).ToArray()

我在下面的类中存储了一些静态数据。我还有一些方法可以使用LINQ过滤这些数据。像这样:

static class Data {

    // ...

    static public Weapon[] weapons = new Weapon[] {
        // ...
    }

    static public GetUnlockedWeapons() {
        Data.weapons.Where( a => a.unlocked ).ToArray();
    }

    static public GetWeaponsType(string type) {
        Data.weapons.Where( a => a.type == type ).ToArray();
    }

// ...
}
但现在我看到这种方式不是很灵活。当我构建更多的过滤器时,组合它们是相当困难和/或冗长的

我来自RubyonRails开发,在那里ActiveRecord有类似于
作用域的东西,它们是可链接的

我想知道是否有什么方法可以
链接我的过滤器,方法类似于:

weapons = Data.weapons.unlocked().type("sword").andMore().andEvenMore();
然后你可以像这样使用它:

weapons = weapons.unlocked().type("sword");
然后你可以像这样使用它:

weapons = weapons.unlocked().type("sword");

是的,您可以这样做:

static class Data {

    // ...

    static public Weapon[] weapons = new Weapon[] {
        // ...
    }

    public static IEnumerable<Weapon> GetUnlockedWeapons(this IEnumerable<Weapon> weapons) {
        return weapons.Where( a => a.unlocked );
    }

    public static IEnumerable<Weapon> GetWeaponsType(this IEnumerable<Weapon> weapons, string type) {
        return weapons.Where( a => a.type == type );
    }

// ...
}
Weapon[] weapons = new Weapon[] 
{
    // ...
};

weapons = new Data(weapons).unlocked().type("sword").andMore().andEvenMore().Weapons;

返回数组是没有意义的,因为您很可能会将其链接起来。将集合的具体化留给调用方(在本例中,我们使用的是
ToList

是的,您可以这样做:

static class Data {

    // ...

    static public Weapon[] weapons = new Weapon[] {
        // ...
    }

    public static IEnumerable<Weapon> GetUnlockedWeapons(this IEnumerable<Weapon> weapons) {
        return weapons.Where( a => a.unlocked );
    }

    public static IEnumerable<Weapon> GetWeaponsType(this IEnumerable<Weapon> weapons, string type) {
        return weapons.Where( a => a.type == type );
    }

// ...
}
Weapon[] weapons = new Weapon[] 
{
    // ...
};

weapons = new Data(weapons).unlocked().type("sword").andMore().andEvenMore().Weapons;

返回数组是没有意义的,因为您很可能会将其链接起来。将集合的具体化留给调用方(在本例中,我们使用的是
ToList

如何定义有关
IEnumerable
类型的过滤器:

public static class Data 
{
    public Weapon[] GetUnlockedWeapons(this IEnumerable<Weapon> weapons)
    {
        return weapons.Where( a => a.unlocked ).ToArray();
    }

    public static Weapon[] GetWeaponsType(this IEnumerable<Weapon> weapons, string type)
    {
        return weapons.Where( a => a.type == type ).ToArray();
    }
}
或者,您可以定义一个使用所谓fluent接口的API:


将过滤器定义为
IEnumerable
类型如何:

public static class Data 
{
    public Weapon[] GetUnlockedWeapons(this IEnumerable<Weapon> weapons)
    {
        return weapons.Where( a => a.unlocked ).ToArray();
    }

    public static Weapon[] GetWeaponsType(this IEnumerable<Weapon> weapons, string type)
    {
        return weapons.Where( a => a.type == type ).ToArray();
    }
}
或者,您可以定义一个使用所谓fluent接口的API:


将您的阵列武器[]更改为提供更多筛选选项的列表。您能给我一个列表更好的示例吗?列表是添加了许多其他方法的增强阵列。只需创建一个列表对象,然后查看可用的方法。将您的数组武器[]更改为提供更多筛选选项的列表。您能给我一个列表更好的示例吗?列表是添加了许多其他方法的增强数组。只需创建一个列表对象,然后查看可用的方法。我在
GetUnlockedWeapons
中看到两次单词
public
。错别字?是的,对不起-第二个应该删除,工作完美无瑕。谢谢我在
GetUnlockedWeapons
中看到两次单词
public
。错别字?是的,对不起-第二个应该删除,工作完美无瑕。谢谢