从没有ForEach循环的相邻单元格获取DataTable值?

时间:2018-01-30 22:43:01

标签: c# sql foreach faircom-db-sql

我正在解决这个问题:Create multiple results from single row joining either 2 or 3 tables based off conditional of table1 results?虽然我希望我可以提出Strawberry's建议,但我不能这样做,所以我现在正在尝试用C#做更多事情而不是通过数据库,但也试图了解我利用了多少CPU。

对于比例,table1可能有50,000条记录,其中包含20个codetype字段,必须先进行评估才能匹配table2(有大约2,000行)或table3(可能有200,000行)。为了避免锤击数据库,我将存储内存中可能的内容,尽可能地按日期限制结果,但我想避免每20个代码类型匹配2,000个foreach循环。

首先,我从table2获取我需要的结果并将它们加载到存储为名为descriptionLookup的变量的C#DataTable中:

id,    description
13    Item 13 Description
15    Item 15 Description
17    Item 17 Description
18    Item 18 Description
21    Item 21 Description
28    Item 28 Description
45    Item 45 Description

table3为lookupTable

id,  table2id
1    15
33   17
21   28

做一个简单的(不显示所有周围的代码,只是相关):

var rawData = new DataTable();
using (OdbcCommand com = new OdbcCommand("SELECT id, description from table2", conn))
{
    using (OdbcDataReader reader = com.ExecuteReader())
    {
        rawData.Load(reader);
        conn.Close();
        return rawData;
    }

}

然后我将其分配给调用该函数的变量。现在我必须处理table1:

codeid1,codeid2,codeid3,...codeid20 ... codetype1,codetype2,codetype3,.....codetype20
18      13      1          33           0         0         1              1
13      21      45         0            0         1         0              0

使用foreach行,我需要为每个codetype列评估1或0.当codetype = 1时,我需要获取相关的codeid,然后从我在内存中保存的数据中查找{{ 1}}查看table2id与descriptionLookup中的id匹配的内容,然后使用它来查找table2中相关字段的描述。

如果codetype为0,我只需要将lookuptable与table2中的相关描述字段匹配。

我正在研究如何解决这个问题,我能想到的只有:

codeid

我还没有尝试运行上面的代码,因此可能存在一两个问题,但它可以使用DataTable descriptionLookup= DB.ExecuteQuery("SELECT id, description from table2"); DataTable lookupTable= DB.ExecuteQuery("SELECT id, table2id from table3"); DataTable mainData= DB.ExecuteQuery("SELECT * from from table1"); foreach (var row in mainData) { var manDataId = row.GetColumn("id"); var subroutine = new Dictionary<string, string>(); for (var index = 1; index < 20; index++) { string description; if (row.GetColumn("codetype" + index) == "1") { int idLookup = row.GetColumn(["codeid" +index]); foreach (var row2 in lookupTable) { if (row3.GetColumn("id") == idLookup) { descriptionId = row3.GetColumn("table2id"); foreach (var row2 in descriptionLookup) { if (row.GetColumn("id") == descriptionId) { description = row2.GetColumn("description").ToString(); } } } } }elseif (row.GetColumn("codetype" + index) == "0") { descriptionId = row.GetColumn(["codeid" +index]); foreach (var row2 in descriptionLookup) { if (row.GetColumn("id") == descriptionId) { description = row2.GetColumn("description").ToString(); } } } subroutine.Add(manDataId.ToString(), description.ToString()); } ArrayData.Add(subroutine); } 循环遍历数千条记录。替代方案似乎是在进行数据库查询,但这似乎比仅仅循环内存中的内容更为密集,但似乎有一种更好的方法可以解决如何获取foreach (var row3 in idLookup)或{{{ 1}}不使用foreach。

使这成为历史上最长的问题:)这是我到目前为止的SQL:

id

只要没有table2id的任何一个有1,这就行得很好,否则会跳过该记录。可能没有一行会填写所有SELECT table1.id, table1.field1, table1.field2, table2.description, fee.amt as fee FROM table2 INNER JOIN table1 ON table2.id = table1.codeid1 OR table2.id = table1.codeid2 OR table2.id = table1.codeid3 OR table2.id = table1.codeid4 OR table2.id = table1.codeid5 OR table2.id = table1.codeid6 OR table2.id = table1.codeid7 OR table2.id = table1.codeid8 OR table2.id = table1.codeid9 OR table2.id = table1.codeid10 OR table2.id = table1.codeid11 OR table2.id = table1.codeid12 OR table2.id = table1.codeid13 OR table2.id = table1.codeid14 OR table2.id = table1.codeid15 OR table2.id = table1.codeid16 OR table2.id = table1.codeid17 OR table2.id = table1.codeid18 OR table2.id = table1.codeid19 OR table2.id = table1.codeid20 INNER JOIN fee ON table2.id = fee.id WHERE table1.codetype1 = 0 AND table1.codetype2 = 0 AND table1.codetype3 = 0 AND table1.codetype4 = 0 AND table1.codetype5 = 0 AND table1.codetype6 = 0 AND table1.codetype7 = 0 AND table1.codetype8 = 0 AND table1.codetype9 = 0 AND table1.codetype10 = 0 AND table1.codetype11 = 0 AND table1.codetype12 = 0 AND table1.codetype13 = 0 AND table1.codetype14 = 0 AND table1.codetype15 = 0 AND table1.codetype16 = 0 AND table1.codetype17 = 0 AND table1.codetype18 = 0 AND table1.codetype19 = 0 AND table1.codetype20 = 0 ,也不会出现codetype全局为1以匹配此查询的反转的情况。

0 个答案:

没有答案