Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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/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
C# 让序列对列表起同样的作用_C#_Linq - Fatal编程技术网

C# 让序列对列表起同样的作用

C# 让序列对列表起同样的作用,c#,linq,C#,Linq,我有一个班叫Country。它有公众成员、“国家名称”和“州” 我已经公布了一份国家名单 现在我想写一个函数,它接受一个新的'Country',并决定CountryList是否已经有'Country' 我试着写一个函数,比如 bool CheckCountry(Country c) { return CountryList.Exists(p => p.CountryName == c.Name && p

我有一个班叫Country。它有公众成员、“国家名称”和“州”

我已经公布了一份国家名单

现在我想写一个函数,它接受一个新的'Country',并决定CountryList是否已经有'Country'

我试着写一个函数,比如

bool CheckCountry(Country c)
{
    return CountryList.Exists(p => p.CountryName == c.Name
                                && p.States.SequenceEqual(c.States));
}

当我想使用州的CountryName属性来比较州时,我想修改我的函数,以便SequenceEqual根据州的CountryName工作?

您有没有研究过在项上实现IComparer?

将其分解为许多简单的查询,然后将这些查询放回一起

让我们从按名称匹配的项目序列开始:

var nameMatches = from item in itemList where item.Name == p.Name select item;
我们需要将这些项与p的子项中的名称序列进行比较。那是什么顺序编写查询:

var pnames = from subitem in p.SubItems select subitem.Name;
现在,您希望从名称序列匹配的名称匹配中查找所有元素。你打算如何得到名字的顺序?好的,我们刚刚看到了如何使用pnames来实现这一点,所以做同样的事情:

var matches = from item in nameMatches
              let subitemNames = 
                  (from subitem in item.SubItems select subitem.Name)
              where pnames.SequenceEqual(subitemNames)
              select item;
现在你想知道,有匹配的吗

return matches.Any();
这应该可以正常工作。但是如果你想成为一个真正的buff,你可以在一个大的查询中写下全部内容

return (
    from item in itemList
    let pnames = 
        (from psubitem in p.SubItems select psubitem.Name)
    let subitemNames = 
        (from subitem in item.SubItems select subitem.Name)
    where item.Name == p.Name
    where pnames.SequenceEqual(subitemNames)
    select item).Any();

你完成了。容易极了!请记住,将其分解为几个小步骤,分别解决每个问题,然后根据小结果将解决方案组合在一起。

如果我理解正确,您需要一种比较两个项目的方法,首先检查两个项目的名称,然后依次检查每个子项目的名称。以下是您想要的:

    public override bool Equals(object obj)
    {
        return this.Name == (obj as Item).Name;
    }
    public override int GetHashCode()
    {
        return this.Name.GetHashCode();
    }
    public bool Check(Item obj)
    {
        if (this.Name != obj.Name)
            return false;
        //if the lists arent of the same length then they 
        //obviously dont contain the same items, and besides 
        //there would be an exception on the next check
        if (this.SubItems.Count != obj.SubItems.Count)
            return false;
        for (int i = 0; i < this.SubItems.Count; i++)
            if (this.SubItems[i] != obj.SubItems[i])
                return false;
        return true;
    }
public override bool Equals(对象对象对象)
{
返回此.Name==(对象作为项).Name;
}
公共覆盖int GetHashCode()
{
返回此.Name.GetHashCode();
}
公共边界检查(项目obj)
{
if(this.Name!=obj.Name)
返回false;
//如果列表的长度不同,则它们
//显然不包含相同的项目,而且
//下次检查时会有例外
if(this.SubItems.Count!=obj.SubItems.Count)
返回false;
对于(int i=0;i
我真的很想知道这是不是C团队的某个人在阅读时写的。结果是:埃里克·利珀特当然是C队的!