以类似Rails范围的方式链接C#LINQ
我在下面的类中存储了一些静态数据。我还有一些方法可以使用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()
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
。错别字?是的,对不起-第二个应该删除,工作完美无瑕。谢谢