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# Linq从一个表中查询匹配列并填充到另一个表中_C#_Linq - Fatal编程技术网

C# Linq从一个表中查询匹配列并填充到另一个表中

C# Linq从一个表中查询匹配列并填充到另一个表中,c#,linq,C#,Linq,我的表格如下: 身份证件 名字 姓 1. 约翰 默里 2. 史密斯 默里 3. 娜塔莎 默里 4. 史蒂夫 凯 5. 账单 凯 让我们定义数据的类 class Table { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } 这应该是你问题的一部分。现在填写数据 Table[] table = new Table

我的表格如下:

身份证件 名字 姓 1. 约翰 默里 2. 史密斯 默里 3. 娜塔莎 默里 4. 史蒂夫 凯 5. 账单 凯
让我们定义数据的类

class Table
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
这应该是你问题的一部分。现在填写数据

Table[] table = new Table[]
{
    new Table{ Id = 1, FirstName = "John", LastName = "Murray" },
    new Table{ Id = 2, FirstName = "Smith", LastName = "Murray" },
    new Table{ Id = 3, FirstName = "Natasha", LastName = "Murray" },
    new Table{ Id = 4, FirstName = "Steve", LastName = "Kay" },
    new Table{ Id = 5, FirstName = "Bill", LastName = "Kay" }
};
这也是你问题的一部分。如果你忽略了这一点,你会大大降低获得aswer的机会。现在,只需将表本身与姓氏连接起来

var query = from t1 in table
            from t2 in table
            where t1.LastName == t2.LastName && t1.Id != t2.Id
            select new { t2.Id, Name = t1.FirstName, MatchingName = t2.FirstName };

foreach (var row in query.Where(t => t.Name == "John"))
{
    Console.WriteLine(row);
}

 { Id = 2, Name = John, MatchingName = Smith }
 { Id = 3, Name = John, MatchingName = Natasha }

唯一不同的是Id,从提供的数据来看,我不知道John或Natasha与Id号2有何关联。

是SQL Server表吗?是的。。我想在linq中查询它,以便在我的类中使用它,就像简单的自连接一样。你尝试了什么?我为此写了一个sql查询。。。但我想把它转换成linq。。select*from table d inner join select lastName from table,其中lastName='Murray'group by lastName在dup.lastName=d.lastName上的计数不同firstName>1 dup;