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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
C# 在所有类列表中搜索匹配值<;对象>;_C#_Linq - Fatal编程技术网

C# 在所有类列表中搜索匹配值<;对象>;

C# 在所有类列表中搜索匹配值<;对象>;,c#,linq,C#,Linq,我有两个对象,一个引用另一个。我希望能够使用类似于Player.Inventory.Contain(Item.Attributes==“Sharp”)的东西。我的目标是能够扫描所有物品属性的玩家清单,并检查一个或多个或没有匹配。通过这种方式,我可以根据角色清单动态更改发生的情况 class Player { public string Name { get; set; } public List<Item> Inventory { get; set; } p

我有两个对象,一个引用另一个。我希望能够使用类似于
Player.Inventory.Contain(Item.Attributes==“Sharp”)
的东西。我的目标是能够扫描所有物品属性的玩家清单,并检查一个或多个或没有匹配。通过这种方式,我可以根据角色清单动态更改发生的情况

class Player
{
    public string Name { get; set; }
    public List<Item> Inventory { get; set; }

    public Player()
    {
        Inventory = new List<Item>();
    }
}
职业玩家
{
公共字符串名称{get;set;}
公共列表清单{get;set;}
公共玩家()
{
库存=新列表();
}
}
以及:

公共类项目
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共布尔值为{get;set;}
公共列表属性{get;set;}
公共项(整数id、字符串名称)
{
ID=ID;
名称=名称;
属性=新列表();
}
公共项(int-id、字符串名、bool-iscarried)
{
ID=ID;
名称=名称;
IsCarried=IsCarried;
属性=新列表();
}
}

看起来您可以使用带有lambda函数的LINQ查询来实现此功能。 这是一个可以在播放器类中实现的函数,用于在项目上查询具有特定属性名称的项目

  • 带有
    IEnumerable的只读解决方案

    public IEnumerable FindMatchingItems(字符串attributeName){
    返回this.Items.Where(x=>x.Name==attributeName).AsEnumerable();
    }
    
  • 列表列出解决方案

    公共列表FindMatchingItems(字符串attributeName){
    返回this.Items.Where(x=>x.Name==attributeName.ToList();
    }
    

适当的LINQ运算符是
.Any()
。即

player.Inventory.Any(item => item.Attributes.Contains("Sharp"))

请注意,如果属性数量变大,则性能会变差。对于
属性
,您应该更喜欢
哈希集
,而不是
列表
,或者如果同一属性可以出现多次,您应该更喜欢
字典。

啊,我的错误。OP可以在.Where()末尾使用fluent API.ToList(),或者如果只需要只读,他可以将方法签名更改为IEnumerable。感谢您的建议!我更新了我的答案,给出了两种方法。在这种情况下,项目代表什么?我试过了,但不起作用。不包含“Items”的定义是我遇到的错误。请注意,如果使用哈希集,则需要将Any()表达式更改为包含。LINQ依赖其ienumerable实现来执行O(n)的任何()操作。Contains()是O(1)。由于内部谓词已经使用Contains,因此上述语法仍然适用于
HashSet
。但我在回复中混淆了属性项-更正。
public IEnumerable<Item> FindMatchingItems(string attributeName) {
    return this.Items.Where(x => x.Name == attributeName).AsEnumerable();
}
public List<Item> FindMatchingItems(string attributeName) {
    return this.Items.Where(x => x.Name == attributeName).ToList();
}
player.Inventory.Any(item => item.Attributes.Contains("Sharp"))