English 中文(简体)
LEFT OU JOIN 2 数据表
原标题:LEFT OUTER JOIN 2 datatables
  • 时间:2012-05-01 19:20:24
  •  标签:
  • c#
  • linq

I m trying to understand how to query, preferably with LINQ, 2 datatables. I would like to do a LEFT OUTER JOIN on them

Datatable1 with col s: [ID] [colA]
DataTable2 with col s: [ID] [ColB] [ColC] ...

期待着加入该《发展纲领》。

请允许我举一个例子,以便我能够将其应用于我所拥有的数据表? 事先感谢你

最佳回答

这汇编了你预期的以下成果:

// create the default row to be used when no value found
var defaultRow = DataTable2.NewRow();
defaultRow[0] = 0;
defaultRow[1] = String.Empty;

// the query
var result = from x in DataTable1.AsEnumerable()
    join y in DataTable2.AsEnumerable() on (string)x["ID"] equals (string)y["ID"] 
             into DataGroup
    from row in DataGroup.DefaultIfEmpty<DataRow>(defaultRow)
    select new {a = x["ColA"], b = (string)row["ColB"]};
问题回答

查阅LEFT OUTER Join

and you should try using @Joanna link.

from x in DataTable1
join y in DataTable2 on x.ID equals y.ID into DataGroup
from item in DataGroup.DefaultIfEmpty(new y.ColB = String.Empty , y.ColC = String.Empty})
select new {x.ColA, item.ColB , item.ColC}

<>>>>>

鉴于你应研究准则-Dataset的文章

这里是法典。

DataTable DataTable1 = new DataTable();
DataTable DataTable2 = new DataTable();

DataTable1.Columns.Add("ID");
DataTable1.Columns.Add("ColA");
DataTable1.Rows.Add(1, "A");
DataTable1.Rows.Add(2, "B");    

DataTable2.Columns.Add("ID");
DataTable2.Columns.Add("ColB");
DataTable2.Rows.Add(1, "B");

var result = from x in DataTable1.AsEnumerable()
             join y in DataTable2.AsEnumerable() on x["ID"] equals y["ID"] into DataGroup                         
             from item in DataGroup.DefaultIfEmpty()
             select new {
                            ID = x["ID"],
                            ColA = x["ColA"],
                            ColB = item == null ? string.Empty : item["ColB"]
                        };
foreach (var s in result)
    Console.WriteLine("{0}", s);

争取一名内人参加这一尝试

from x in Datatable1
join y in Datatable2 on x.ID equals y.ID
select new {x.colA, y.ColB, y.ColC }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签