【问题标题】:How do I match two identical database tables with LINQ?如何使用 LINQ 匹配两个相同的数据库表?
【发布时间】:2009-04-26 04:19:41
【问题描述】:

我想匹配两个相同的表:

sourceProducts (productName, ProductionDate, ManID, shipper, distributer)
CommProducts   (productName, ProductionDate, ManID, shipper, distributer)

但行数和记录内容可能不同。如何从一个表中选择某个记录 = raw 并从另一个表中获取其克隆记录(例如,检查是否存在相同的记录)?如何使用 LinQ 做到这一点?

更新:这是 LINQ 代码:

    protected void checkBtn_Click(object sender, EventArgs e)
    {

        MyProductsDataContext mySdb = new MyProductsDataContext();

        Product   mypro = new Product  { ManId = int.Parse(TxtManI.Text), ProductName = TxtProN.Text, ProductionDate =DateTime .Parse ( TxtProDat.Text), Shipper = TxtShipI.Text, Distributer = TxtDistI.Text };

        var spro = (from p in mySdb.Products
                        select new { p.ManId, p.ProductName, p.ProductionDate, p.Shipper, p.Distributer }).
                        Intersect(from s in mySdb.SourceProducts  select new { s.ManId, s.ProductName, s.ProductionDate, s.Shipper, s.Distributer });

        if (spro != null)
        {
            LblMessage.Text = "Acceptable product Data Inserted Sucessfully";
            InsertData();
        }
        else
        {
            LblMessage.Text = "Invalid Product or bad Entry Please retype";
        }
   }

【问题讨论】:

  • 我更新了您的帖子以包含您粘贴在另一个问题下的代码。
  • 我想知道您为什么要求用户在您已经拥有所有数据的情况下再次输入所有数据。

标签: c# sql linq linq-to-sql


【解决方案1】:

我会加入 ManId,然后比较 where 子句中的其余值:

bool productExists = (
    from p in mySdb.Products
    join s in mySdb.SourceProducts
      on p.ManId equals s.ManId
    where p.ProductName == s.ProductName
       && p.ProductionDate == s.ProductionDate
       && p.Shipper == s.Shipper
       && p.Distributer = s.Distributer
    select new { p.ManId, p.ProductName, p.ProductionDate, p.Shipper, p.Distributer }
    ).Any();

if (productExists)
{
    LblMessage.Text = "Acceptable product Data Inserted Sucessfully";
    InsertData();
}
else
{
    LblMessage.Text = "Invalid Product or bad Entry Please retype";
}

我使用Any() 生成了一个高效的EXISTS SQL 查询。如果您确实需要使用退回的产品,您可以使用SingleOrDefault() 或FirstOrDefault()。

我也没有看到您正在使用新产品 ID 的任何地方 - 您可能还需要将该过滤器添加到查询中:

Product   mypro = new Product  { ... };

bool productExists = (
    from p in mySdb.Products
    where p.ManId equals mypro.ManId
    join s in mySdb.SourceProducts
      on p.ManId equals s.ManId
    ...

【讨论】:

    【解决方案2】:

    您可能可以使用连接来做到这一点,但我已经将一个单元测试拼凑在一起,它显示了一种方法

    public class TestProduct
    {
        public int ManId { get; set; }
        public string ProductName { get; set; }
        public DateTime ProductionDate { get; set; }
        public string Shipper { get; set; }
        public string Distributor { get; set; }
    }
    
    [TestMethod]
    public void TestSourceTable()
    {
        // Set up a test list
        var list = new List<TestProduct>();
        for (int i=0;i<5;i++)
        {
            var p = new TestProduct
                {
                    Distributor = "D" + i,
                    ManId = i,
                    ProductionDate = DateTime.Now,
                    ProductName = "P" + i,
                    Shipper = "S" + i
                };
            list.Add(p);
        }
    
        // Get an existing product
        var existingProduct = list[4];
    
        // Get an unknown product
        var unknownProduct = new TestProduct()
            {
                ManId = -1,
                Distributor = "",
                ProductionDate = DateTime.Now.AddDays(-1),
                ProductName = "",
                Shipper = ""
            };
    
         // product found
         Assert.True(list.Any(p => p == existingProduct));
    
         // product not found
         Assert.False(list.Any(p => p == unknownProduct));
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-22
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2012-08-01
      相关资源
      最近更新 更多