【问题标题】:How to query two tables for matching values with linq and C#?如何使用 linq 和 C# 查询两个表的匹配值?
【发布时间】:2014-06-06 00:52:45
【问题描述】:

我对 C# 和 Linq 还是很陌生,在查询两个表的匹配值时遇到了一些麻烦。

在我作为实习生工作的地方,我们使用工作单元存储库模式。例如,要查询一个 sql 表,我会使用这个 linq 表达式:_entities.Atable.Where(x => x.Col1 == col1 && x.Col2 == col2)_entities 变量代表数据库模型中所有表的接口。

假设我有两个表Table1Table2。以下是每个表的列:

Table1: Col1, Col2, colA, ColB
Table2: Col1, col2, ColC, ColD

这是我想做的:

  • 通过将两个变量 col1 和 col2 与 Col1 和 Col2 匹配来查询 Table1

    例如,List1 = _entities.Table1.Where(x => x.Col1 == col1 && x.Col2 == col2).ToList()

  • 然后,查询 Table2 以获取 Table2.Col1Table2.Col2 匹配 List1.Col1List1.Col2 的任何记录。这些结果也可以存储在一个列表中,List2

    例如,List2 = _entities.Table2.Where(x => x.Col1 == List1.Col1 && x.Col2 == List1.Col2).ToList()

  • 然后,创建第三个列表List3,其中包含来自Table1 的所有项目与来自Table2 的项目不匹配

最后,我想要一个Table2 项目列表,这些项目与Col1Col2 匹配Table1。以及在Table2 中没有匹配的剩余Table1 项目的列表。

大多数情况下,我停留在第三个要点上,但我愿意接受任何建议和改进。

最后,我不知道这是否可能,我想将这两个列表合并为一个。由于它们是不同的类型,我不确定这将如何工作。

我希望这一切都有意义。我一直在努力解决它一段时间,但我仍然对如何创建查询感到困惑。我也玩过Except()Intersect(),但运气不佳。

任何帮助将不胜感激。

【问题讨论】:

  • 试试 'Difference()' 而不是 'Except()'?
  • 好的,我会开始研究它。谢谢。
  • 您是否要进行左内连接?

标签: c# sql linq


【解决方案1】:

我通常发现Any 是匹配来自不同类型的多个值的最清晰的方法。

class Type1
{
    public int Key1 { get; set; }
    public int Key2 { get; set; }
    public string Type1Prop { get; set; }
    public Type1(int key1, int key2, string prop)
    {
        Key1 = key1;
        Key2 = key2;
        Type1Prop = prop;
    }
}

class Type2
{
    public int Key1 { get; set; }
    public int Key2 { get; set; }
    public string Type2Prop { get; set; }
    public Type2(int key1, int key2, string prop)
    {
        Key1 = key1;
        Key2 = key2;
        Type2Prop = prop;
    }
}

public void Main()
{   
    var list1 = new List<Type1>
    {
        new Type1(1,1,"Value"), new Type1(1,2,"Value"), new Type1(2,1,"Value"), new Type1(2,2,"Value")
    };
    var list2 = new List<Type2>
    {
        new Type2(1,1,"Value"), new Type2(2,1,"Value"), new Type2(3,1,"Value")
    };
    var in1ButNot2 = list1.Where(item => !list2.Any(item2 => item2.Key1 == item.Key1 && item2.Key2 == item.Key2)).ToList();
    var in2ButNot1 = list2.Where(item => !list1.Any(item2 => item2.Key1 == item.Key1 && item2.Key2 == item.Key2)).ToList();
    var in1And2 = list2.Where(item => list1.Any(item2 => item2.Key1 == item.Key1 && item2.Key2 == item.Key2)).ToList();
    in1ButNot2.ForEach(item => Console.WriteLine("in1ButNot2 - Key1={0},Key2={1}", item.Key1, item.Key2));
    in2ButNot1.ForEach(item => Console.WriteLine("in2ButNot1 - Key1={0},Key2={1}", item.Key1, item.Key2));
    in1And2.ForEach(item => Console.WriteLine("in1And2 - Key1={0},Key2={1}", item.Key1, item.Key2));
}

这将结束输出以下内容,并向您展示可以与数据相交的所有方式。组合列表是您必须自己弄清楚的事情,如果您只需要键,那么您可以创建一个通用类型,然后将其转换为创建组合列表。

in1ButNot2 - Key1=1,Key2=2
in1ButNot2 - Key1=2,Key2=2
in2ButNot1 - Key1=3,Key2=1
in1And2 - Key1=1,Key2=1
in1And2 - Key1=2,Key2=1

【讨论】:

  • 很好的答案!正是我所希望的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多