加入两个DataTable在C#中使用非匹配数据两个、数据、DataTable

2023-09-05 00:46:46 作者:惜落

我在SQL两个表,我将填充在C#中的数据表。他们SQL表被安置在不同的服务器和不同的DB的。

在第一个表中我有5列

 名称(字符串),AgentStatus(串),TimeInState(双),TaskHandled(双),区(串)
 

第二个表,我有3列

 名称(字符串),CChats(双),ACHATS(双)
 
两个EXCEL表格匹配数据,要怎么匹配 不要录入函数公式的办法,要录入4行参数值的解释

我一直在使用这个链接合并表在C#

内的数据表的加入在C#

不过我遇到的问题是这样的。

表1有59行,1为每个用户。

表2中有25行,2,有一个聊天账号每个用户。

当我在C#它们合并我只得到从表1匹配于表2的25行的比赛我需要显示从表1中的所有行,如果他们有一个排在表2示出了该数据,否则显示0的

我知道在哪里的问题是,它在上面的链接select语句,但我不知道如何解决它在C#

下面是我的code不起作用......列表框仅仅是看到调试输出.....

  DataTable的DT1 =新的DataTable();
        DataTable的DT2 =新的DataTable();

        dt1.Columns.Add(姓名,typeof运算(字符串));
        dt1.Columns.Add(状态,typeof运算(字符串));
        dt1.Columns.Add(时代的typeof(双));
        dt1.Columns.Add(电话的typeof(双));
        dt1.Columns.Add(地区的typeof(字符串));

        dt2.Columns.Add(姓名,typeof运算(字符串));
        dt2.Columns.Add(CChats的typeof(双));
        dt2.Columns.Add(ACHATS的typeof(双));

        的foreach(在_agentStates.Rows的DataRow博士)
        {
            DataRow的行= dt1.NewRow();
            行[名称] =博士[0]的ToString();
            行[状态] =博士[1]的ToString();
            行[时间] = Convert.ToDouble(博士[2]的ToString());
            行[电话] = Convert.ToDouble(DR [3]的ToString());
            行[区域] =博士[4]的ToString();
            dt1.Rows.Add(行);
        }
        的foreach(在_chatCount.Rows的DataRow博士)
        {
            DataRow的行= dt2.NewRow();
            行[名称] =博士[0]的ToString();
            行[CChats] = Convert.ToDouble(DR [1]的ToString());
            行[ACHATS] = Convert.ToDouble(博士[2]的ToString());
            dt2.Rows.Add(行);

        }

        VAR的结果=从dt1.AsEnumerable表1()
                     加入表2中dt2.AsEnumerable()上(串)表1 [名称]等于(字符串)表2 [名称]
                     选择新
                     {
                         名称=(字符串)表2 [名称],
                         状态=(字符串)表1 [状态],
                         时间=(双)表1 [美国时代],
                         呼叫=(双)表1 [电话]
                         地区=(字符串)表1 [区域],
                         CChats =(双)表2 [CChats]
                     };
        的foreach(在结果VAR项)
        {
           listBox1.Items.Add(item.Name ++ item.CChats.ToString());

        }
 

解决方案

您想要做一个 LEFT JOIN 概念(这是一个 OUTER JOIN ,而不是 INNER JOIN )。

  

一个左外连接的结果(或只是左连接)为表A和   乙总是包含了左表(A)的所有记录,即使   加盟条件没有找到任何匹配的记录在正确表   (B)的

     

来源: http://en.wikipedia.org/wiki/Join_(SQL )#Left_outer_join

杰夫·阿特伍德还张贴了一个不同的体面视觉解释连接。

在LINQ这样做有点比SQL更笨拙,而且是这样的:

  VAR LeftJoin =从用户的用户
加入聊天的聊天
在user.Name等于user.Name到JoinedTables
从行JoinedTables.DefaultIfEmpty()
选择新
{
  名称,
  AgentStatus,
  TimeInState,
  TaskHandled,
  地区,
  CChats =聊天!= NULL? chat.CChats:​​0
  ACHATS =聊天!= NULL? chat.AChats:​​0
};
 

来源: 的http:// codingsense。字press.com / 2009/03/08 /左联接,右联接 - 使用 - LINQ /

当然,加入了关于名称不理想 - 希望你确实有一个 ID 在现实世界中,或者真的可以保证名称是既独特又将会始终如一地提供(例如,没有尾随空格或大小写差异)。

//编辑,寻址更新code样品

试试这个:

  VAR的结果=从表1中dt1.AsEnumerable()
             加入表2中dt2.AsEnumerable()
             对(串)表1 [名称]等于(字符串)表2 [名称]
             进入joinedDt
             从表2中joinedDt.DefaultIfEmpty()
             选择新
             {
                 名称=(字符串)表1 [名称],
                 状态=(字符串)表1 [状态],
                 时间=(双)表1 [美国时代],
                 呼叫=(双)表1 [电话]
                 地区=(字符串)表1 [区域],
                 CChats =(!表2 = NULL(双)表2 [CChats]:0)
             };
 

I have two tables in SQL that I populate into DataTables in C#. They SQL tables are housed on different servers and different DB's.

In the first table I have 5 columns

Name(string), AgentStatus(string), TimeInState(double), TaskHandled(double), Region(string)

The second table I have 3 columns

Name(string), CChats(double), AChats(double)

I have been using this link to merge the tables in C#

Inner join of DataTables in C#

However the issue I am having is this.

Table 1 has 59 rows, 1 for each user.

Table 2 has 25 rows, 2 for each user that has a Chat account.

When I merge them in C# I only get the matches from table 1 that match the 25 rows in table 2. I need to show all rows from table1 and if they have a row in table 2 show that data, otherwise display 0's

I know where the issue is, its in the select statement in the link above, but I am not sure how to fix it in C#

Here is my code that does not work......The listbox is just to see the output for debugging.....

        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();

        dt1.Columns.Add("Name", typeof(string));
        dt1.Columns.Add("Status", typeof(string));
        dt1.Columns.Add("Time", typeof(double));
        dt1.Columns.Add("Calls", typeof(double));
        dt1.Columns.Add("Region", typeof(string));

        dt2.Columns.Add("Name", typeof(string));
        dt2.Columns.Add("CChats", typeof(double));
        dt2.Columns.Add("AChats", typeof(double));

        foreach(DataRow dr in _agentStates.Rows)
        {
            DataRow row = dt1.NewRow();
            row["Name"] = dr[0].ToString();
            row["Status"] = dr[1].ToString();
            row["Time"] = Convert.ToDouble(dr[2].ToString());
            row["Calls"] = Convert.ToDouble(dr[3].ToString());
            row["Region"] = dr[4].ToString();
            dt1.Rows.Add(row);
        }
        foreach(DataRow dr in _chatCount.Rows)
        {
            DataRow row = dt2.NewRow();
            row["Name"] = dr[0].ToString();
            row["CChats"] = Convert.ToDouble(dr[1].ToString());
            row["AChats"] = Convert.ToDouble(dr[2].ToString());
            dt2.Rows.Add(row);

        }

        var result = from table1 in dt1.AsEnumerable()
                     join table2 in dt2.AsEnumerable() on (string)table1["Name"] equals (string)table2["Name"]
                     select new
                     {
                         Name = (string)table2["Name"],
                         Status = (string)table1["Status"],
                         Time = (double)table1["Time"],
                         Calls = (double)table1["Calls"],
                         Region = (string)table1["Region"],
                         CChats = (double)table2["CChats"]
                     };
        foreach (var item in result)
        {
           listBox1.Items.Add(item.Name + " " + item.CChats.ToString());

        }

解决方案

You want to do a LEFT JOIN conceptually (which is an OUTER JOIN, not an INNER JOIN).

The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B).

Source: http://en.wikipedia.org/wiki/Join_(SQL)#Left_outer_join

Jeff Atwood has also posted a decent visual explanation of the different joins.

In LINQ this is done a bit more awkwardly than SQL, and is something like:

var LeftJoin = from user in Users
join chat in Chats
on user.Name equals user.Name into JoinedTables
from row in JoinedTables.DefaultIfEmpty()
select new                          
{
  Name,
  AgentStatus,
  TimeInState,
  TaskHandled,
  Region,
  CChats = chat != null ? chat.CChats : 0
  AChats = chat != null ? chat.AChats : 0                          
};

Source: http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/

Of course joining on Name isn't ideal - hopefully you actually have an ID in the real world, or can really guarantee that names are both unique and will be provided consistently (e.g. no trailing whitespace or differences in capitalisation).

// EDIT, Addressing updated code sample

Try this:

var result = from table1 in dt1.AsEnumerable()
             join table2 in dt2.AsEnumerable() 
             on (string)table1["Name"] equals (string)table2["Name"]
             into joinedDt
             from table2 in joinedDt.DefaultIfEmpty()
             select new
             {
                 Name = (string)table1["Name"],
                 Status = (string)table1["Status"],
                 Time = (double)table1["Time"],
                 Calls = (double)table1["Calls"],
                 Region = (string)table1["Region"],
                 CChats = (table2 != null ? (double)table2["CChats"] : 0)
             };

 
精彩推荐