C# 使用linq从另一个表中存在的表中比较和检索值集

C# 使用linq从另一个表中存在的表中比较和检索值集,c#,sql,linq,duplicates,C#,Sql,Linq,Duplicates,我想看看表1中是否有任何“行”包含在表2中,并使用LINQ从表2中提取它们 示例场景: table { int ID int RowID int ColumnID string cellvalue } 表1 1, 1, 1, A 2, 1, 2, B 3, 2, 1, C 4, 2, 2, D 代表 A | B C | D X | Y A | C A | B A | B 表2 1, 1, 1, X 2, 1, 2, Y 3, 6, 1, A 4, 6, 2, C

我想看看表1中是否有任何“行”包含在表2中,并使用LINQ从表2中提取它们

示例场景:

table {
   int ID
   int RowID
   int ColumnID
   string cellvalue
}
表1

1, 1, 1, A
2, 1, 2, B
3, 2, 1, C
4, 2, 2, D
代表

A | B
C | D
X | Y
A | C
A | B
A | B
表2

1, 1, 1, X
2, 1, 2, Y
3, 6, 1, A
4, 6, 2, C
5, 8, 1, A
6, 8, 2, B
7, 9, 1, A
8, 9, 2, B
代表

A | B
C | D
X | Y
A | C
A | B
A | B
所以在上面的例子中,表1中的(A | B)也在表2中(A | B)两次

我想从表2中得到的结果是:

5, 8, 1, A
6, 8, 2, B
7, 9, 1, A
8, 9, 2, B
我试图在另一个查询中使用GROUPBY,然后使用contains,但我迷路了。我在想,如果我将其旋转以形成实际的行,可能会更容易,但LINQ中没有旋转

如果有人能帮忙的话,我很可能跳过了一些简单的事情?如果有更好的答案,不一定是LINQ


干杯

我不知道LINQ,但SQL查询将是:

Select T2.Id, T2.RowID, T2.ColumnID, T2.CellValue 
From Table12 T2 
where 
  exists(
    Select 1 From Table1 T1 
      where T1.ID=T2.ID and 
            T1.RowID=T2.RowID and 
            T1.ColumnID=T2.ColunID and 
            T1.CellValue=T2.CellValue
  ) 
尝试一下:

备选方案1:

List<Table> table = new List<Table>() { 
    new Table(1, 1, 1, "A"),
    new Table(2, 1, 2, "B"),
    new Table(3, 2, 1, "C"),
    new Table(4, 2, 2, "D")
};

List<string> rowsTable1 = table.GroupBy(x => x.RowID)
                               .Select(x => string.Join(",", x.Select(y => y.Cellvalue).ToArray()))
                               .ToList();


List<Table> table2 = new List<Table>() { 
    new Table(1, 1, 1, "X"),
    new Table(2, 1, 2, "Y"),
    new Table(3, 6, 1, "A"),
    new Table(4, 6, 2, "C"),
    new Table(5, 8, 1, "A"),
    new Table(6, 8, 2, "B"),
    new Table(7, 9, 1, "A"),
    new Table(8, 9, 2, "B")
};

List<Table> table3 = table2.GroupBy(x => x.RowID)
                           .Where(x => rowsTable1.Contains(string.Join(",", x.Select(y => y.Cellvalue).ToArray())))
                           .SelectMany(x => x.Select(y => y))
                           .ToList();
public class Table 
{
   public int ID {get;set;}
   public int RowID{get;set;}
   public int ColumnID{get;set;}
   public string Cellvalue { get; set; }

   public Table(int id, int rowid, int columnid, string cellvalue)
   {
       ID = id;
       RowID = rowid;
       ColumnID = columnid;
       Cellvalue = cellvalue;
   }
}

public class TableRow
{
    public List<string> Values { get; set; }

    public TableRow (IGrouping<int,Table> group)
    {
        Values = group.OrderBy(y => y.ColumnID)
                      .Select(y => y.Cellvalue)
                      .ToList();
    }

    public override bool Equals(object obj)
    {
        TableRow row = obj as TableRow;
        if (row != null)
        {
            if (row.Values != null && row.Values.Count == Values.Count)
            {
                for (int i = 0; i < Values.Count; i++)
                {
                    if (Values[i] != row.Values[i])
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        return base.Equals(obj);
    }
}


List<Table> table = new List<Table>() { 
    new Table(1, 1, 1, "A"),
    new Table(2, 1, 2, "B"),
    new Table(3, 2, 1, "C"),
    new Table(4, 2, 2, "D")
};

List<TableRow> rowsTable1 = table.GroupBy(x => x.RowID)
                                 .Select(x => new TableRow(x))
                                 .ToList();

List<Table> table2 = new List<Table>() { 
    new Table(1, 1, 1, "X"),
    new Table(2, 1, 2, "Y"),
    new Table(3, 6, 1, "A"),
    new Table(4, 6, 2, "C"),
    new Table(5, 8, 1, "A"),
    new Table(6, 8, 2, "B"),
    new Table(7, 9, 1, "A"),
    new Table(8, 9, 2, "B")
};

List<Table> table3 = table2.GroupBy(x => x.RowID)
                           .Where(x => rowsTable1.Exists(y=> y.Equals(new TableRow(x))))
                           .SelectMany(x =>  (IEnumerable<Table>)x)
                           .ToList();
List table=new List(){
新表(1,1,1,“A”),
新表(2,1,2,“B”),
新表(3,2,1,“C”),
新表(4,2,2,“D”)
};
List rowsTable1=table.GroupBy(x=>x.RowID)
.Select(x=>string.Join(“,”,x.Select(y=>y.Cellvalue).ToArray())
.ToList();
List table2=新列表(){
新表(1,1,1,“X”),
新表(2,1,2,“Y”),
新表(3,6,1,“A”),
新表(4,6,2,“C”),
新表(5,8,1,“A”),
新表(6,8,2,“B”),
新表(7,9,1,“A”),
新表(8、9、2,“B”)
};
列表table3=table2.GroupBy(x=>x.RowID)
.Where(x=>rowsTable1.Contains(string.Join(“,”,x.Select(y=>y.Cellvalue.ToArray()))
.SelectMany(x=>x.Select(y=>y))
.ToList();
它通过将行连接到一个由“,”分隔的字符串来组合行。它使用这个组合来比较它们,并在第二个表中找到这些行

此代码不考虑将项目排序的可能性不同。但这可以根据需要进行调整

备选方案2:

List<Table> table = new List<Table>() { 
    new Table(1, 1, 1, "A"),
    new Table(2, 1, 2, "B"),
    new Table(3, 2, 1, "C"),
    new Table(4, 2, 2, "D")
};

List<string> rowsTable1 = table.GroupBy(x => x.RowID)
                               .Select(x => string.Join(",", x.Select(y => y.Cellvalue).ToArray()))
                               .ToList();


List<Table> table2 = new List<Table>() { 
    new Table(1, 1, 1, "X"),
    new Table(2, 1, 2, "Y"),
    new Table(3, 6, 1, "A"),
    new Table(4, 6, 2, "C"),
    new Table(5, 8, 1, "A"),
    new Table(6, 8, 2, "B"),
    new Table(7, 9, 1, "A"),
    new Table(8, 9, 2, "B")
};

List<Table> table3 = table2.GroupBy(x => x.RowID)
                           .Where(x => rowsTable1.Contains(string.Join(",", x.Select(y => y.Cellvalue).ToArray())))
                           .SelectMany(x => x.Select(y => y))
                           .ToList();
public class Table 
{
   public int ID {get;set;}
   public int RowID{get;set;}
   public int ColumnID{get;set;}
   public string Cellvalue { get; set; }

   public Table(int id, int rowid, int columnid, string cellvalue)
   {
       ID = id;
       RowID = rowid;
       ColumnID = columnid;
       Cellvalue = cellvalue;
   }
}

public class TableRow
{
    public List<string> Values { get; set; }

    public TableRow (IGrouping<int,Table> group)
    {
        Values = group.OrderBy(y => y.ColumnID)
                      .Select(y => y.Cellvalue)
                      .ToList();
    }

    public override bool Equals(object obj)
    {
        TableRow row = obj as TableRow;
        if (row != null)
        {
            if (row.Values != null && row.Values.Count == Values.Count)
            {
                for (int i = 0; i < Values.Count; i++)
                {
                    if (Values[i] != row.Values[i])
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        return base.Equals(obj);
    }
}


List<Table> table = new List<Table>() { 
    new Table(1, 1, 1, "A"),
    new Table(2, 1, 2, "B"),
    new Table(3, 2, 1, "C"),
    new Table(4, 2, 2, "D")
};

List<TableRow> rowsTable1 = table.GroupBy(x => x.RowID)
                                 .Select(x => new TableRow(x))
                                 .ToList();

List<Table> table2 = new List<Table>() { 
    new Table(1, 1, 1, "X"),
    new Table(2, 1, 2, "Y"),
    new Table(3, 6, 1, "A"),
    new Table(4, 6, 2, "C"),
    new Table(5, 8, 1, "A"),
    new Table(6, 8, 2, "B"),
    new Table(7, 9, 1, "A"),
    new Table(8, 9, 2, "B")
};

List<Table> table3 = table2.GroupBy(x => x.RowID)
                           .Where(x => rowsTable1.Exists(y=> y.Equals(new TableRow(x))))
                           .SelectMany(x =>  (IEnumerable<Table>)x)
                           .ToList();
公共类表
{
公共int ID{get;set;}
public int RowID{get;set;}
public int ColumnID{get;set;}
公共字符串Cellvalue{get;set;}
公共表(int-id、int-rowid、int-columnid、string-cellvalue)
{
ID=ID;
RowID=RowID;
ColumnID=ColumnID;
Cellvalue=Cellvalue;
}
}
公共类表格行
{
公共列表值{get;set;}
公共表格行(I分组组)
{
Values=group.OrderBy(y=>y.ColumnID)
.选择(y=>y.Cellvalue)
.ToList();
}
公共覆盖布尔等于(对象对象对象)
{
TableRow row=对象作为TableRow;
如果(行!=null)
{
if(row.Values!=null&&row.Values.Count==Values.Count)
{
for(int i=0;ix.RowID)
.选择(x=>new TableRow(x))
.ToList();
List table2=新列表(){
新表(1,1,1,“X”),
新表(2,1,2,“Y”),
新表(3,6,1,“A”),
新表(4,6,2,“C”),
新表(5,8,1,“A”),
新表(6,8,2,“B”),
新表(7,9,1,“A”),
新表(8、9、2,“B”)
};
列表table3=table2.GroupBy(x=>x.RowID)
其中(x=>rowsTable1.Exists(y=>y.Equals(新表行(x)))
.SelectMany(x=>(IEnumerable)x)
.ToList();

这里的规则不是很清楚,如果表1中没有
A | B
,但是另一对,比如
A | D
?,如果你有一个3的匹配,比如
A | B | C
?。@KingKing如果表1中有
A | D
,它将与表2中的任何东西都不匹配。在部分匹配的情况下,表1中的
A | B
将匹配表2中的
A | B | C
,需要检索完整的
A | B | C
,但
A | B
将不匹配,因为列ID需要对齐(但ID和ROWID不需要在表之间匹配)。ID和RowID不需要在表之间匹配。在上面的示例中,表1中的
A | B
作为rowid1存在。而
A | B
在表2中作为行ID 8和行ID 9存在。不幸的是,这行不通。