可以使用Linq在Sql和DataTable之间编写连接吗?

时间:2010-12-15 13:22:51

标签: c# linq-to-sql .net-3.5 datatable linq-to-objects

我有一个根据时间戳从多个数据库(MySql)中提取客户信息的流程。我将这些数据存储到DataTable。数据表表示现有客户信息的更新以及新客户信息。

我想根据一个常量值CompanyIDCustomerID删除目标数据库(SqlServer)中的任何欺骗。所以,我认为 join 会给我目标数据库中dupes的RecordID,将List<int> (或某些收集机制)传递给{{ 1}}方法。

我有什么:

DELETE

这显然不起作用。我将稍微更新确切的错误消息,但这不会编译。

首先,我的连接语法是否符合我想要达到的目的?

其次,如何编写此Linq以在DataTable和目标SqlServer数据库之间进行连接?

事后补充 - 一旦我有一个dupe RecordID集合,是否可以使用Linq来删除目标数据库中的记录?

修改 为了澄清这个过程,我有这样的传入数据表并包含在using (var context = new DataContext(SqlConnection)) { var tblSource = context.GetTable<tblCustomerInfo>(); var dupeIDs = from currCust in tblSource join newCust in myTable.AsEnumerable() on currCust.CompanyID equals newCust.Field<string>("CompanyID") where currCust.CustomerID.Equals(newCust.Field<int>("CustomerID") select currCust.RecordID; }

DataSet

哪一个都将进入一个数据库:

Table1
CompanyID    CustomerID    Field1    Field2    ....
   1             5          ...       ...
   1             15         ...       ...

Table2
CompanyID    CustomerID    Field1    Field2    ....
   10           125         ...       ...
   10           145         ...       ...

因此,在这种情况下,我会从目标表中删除与表1和表1匹配的项目。 2.目标数据库将不断增长,因此创建CustomerID列表似乎不可行。但是,我希望每天导入的新客户信息和更新的客户信息相对较少(数百个,可能接近1000个记录)。

如果我不能写一个连接,那么完成此过程的其他方法是否合适?我试图找出一些东西,因为它看起来我实际上无法混合Linq-to-Sql和Linq-to-Objects。

是否有可能我的数据表映射到实体数据图Destination DB CompanyID CustomerID Field1 Field2 .... 1 5 ... ... 1 15 ... ... 1 27 ... ... 5 15 ... ... 10 125 ... ... 10 145 ... ... 11 100 ... ... ,填充其他不可变的var,然后执行连接?

更新

这是我在这一点上所取得的成就,我得到了我期望从tbl_CustomerInfo得到的结果:

dupes

我现在的问题是, using (DataContext context = new DataContext(SqlConnection) { var custInfo = context.GetTable<tbl_CustomerInfo>(); string compID = ImportCust.Rows[0]["CompanyID"].ToString(); var imports = from cust in ImportCust.AsEnumerable() select cust.Field<int>("CustomerID"); var dupes = from cust in custInfo join import in imports on cust.CustomerID equals import where cust.CompanyID == compID select cust; custInfo.DeleteOnSubmit(/* what goes here */); context.SubmitChanges(); } 的内容是什么?我觉得自己已经变得如此接近,只是被这个挫败了。

3 个答案:

答案 0 :(得分:1)

我通常在存储过程中解决所有这些问题以提高效率。

在目标表中添加标识字段以唯一标识记录,然后使用如下查询:

DELETE d
FROM DestinationTable d JOIN (
   Select CompanyID, CustomerID, Min(UniqueID) AS FirstRecID
    FROM DestinationTable
    GROUP BY CompanyID, CustomerID) u on u.CompanyID=d.CompanyID AND u.CustomerID=d.CustomerID
WHERE d.UniqueID <> u.FirstRecID

答案 1 :(得分:0)

或者,您可以创建两个List<int>列表,其中包含来自两个源的id,然后使用Intersect LINQ运算符查找公共项。

List<int> a = new List<int>{1,2,3,4,5,6,8, 10};
List<int> b = new List<int>{1,2,99,5,6,8, 10};
var c= a.Intersect(b);  //returns the items common to both lists

答案 2 :(得分:0)

这就是我的工作:

using (DataContext context = new DataContext(SqlConnection)
{
    var custInfo = context.GetTable<tbl_CustomerInfo>();

    string compID = ImportCust.Rows[0]["CompanyID"].ToString();

    var imports = from cust in ImportCust.AsEnumerable()
                  select cust.Field<int>("CustomerID");

    var dupes = from import in imports
                join cust in custInfo
                on import equals cust.CustomerID 
                where cust.CompanyID== pivnum
                select cust;

    var records = dupes.GetEnumerator();

    while (records.MoveNext())
    { custInfo.DeleteOnSubmit(records.Current); }

    context.SubmitChanges();
}

如果有更有效的方法,我对选项感兴趣。