簡體   English   中英

執行表連接后在 C# 中處理 lambda 查詢結果

[英]Processing lambda query result in C# after performing table join

所以我有一個將兩個數據表連接在一起的查詢:

var results = (
   from t1 in table1.AsEnumerable()
   join t2 in table2.AsEnumerable() on
        new { a = t1["col1"], b= t1["col2"], c = t1["col3"] } equals
        new { a= t2["col1"], b= t2["col2"], c= t2["col3"] }
   into joinedComboTable
   select joinedComboTable);

它產生一個結果,其類型為IEnumerable<IEnumerable<datarow>>"

我如何將其轉換為DataTable 表 1 和表 2 是 C# DataTable s。 我確實看到了 304 個結果,我可以通過調試器和 results.inner(非公開)參數看到我有DataColumn s,我可以看到 304 行。 但是我無法弄清楚如何獲得實際結果並將其保存到DataTable

更新:2020.01.23 @ 9:04pm

所以,我檢查了下面的幾個選項,當我執行 results.ToList() 時,我基本上得到了 304 個條目的列表,但每行值都是 System.Data.DataRow[0]。 我肯定錯過了什么....

Visual Studio QuickWatch 數據

迭代這個不會產生預期的結果。

嘗試這個

    static void Main(string[] args)
    {
        var table1 = new DataTable();
        table1.Columns.Add("col1", typeof(string));
        table1.Columns.Add("col2", typeof(string));
        table1.Columns.Add("col3", typeof(string));
        table1.Columns.Add("col4", typeof(string));

        var row = table1.NewRow();
        row["col1"] = "1";
        row["col2"] = "1";
        row["col3"] = "1";
        row["col4"] = "something different";
        table1.Rows.Add(row);

        row = table1.NewRow();
        row["col1"] = "2";
        row["col2"] = "2";
        row["col3"] = "2";
        row["col4"] = "something different";
        table1.Rows.Add(row);

        var table2 = new DataTable();
        table2.Columns.Add("col1", typeof(string));
        table2.Columns.Add("col2", typeof(string));
        table2.Columns.Add("col3", typeof(string));
        table2.Columns.Add("col4", typeof(string));

        row = table2.NewRow();
        row["col1"] = "1";
        row["col2"] = "1";
        row["col3"] = "1";
        row["col4"] = "Another different thing";
        table2.Rows.Add(row);

        var results = (
            from t1 in table1.AsEnumerable()
            join t2 in table2.AsEnumerable() on
                new { a = t1["col1"], b = t1["col2"], c = t1["col3"] } equals
                new { a = t2["col1"], b = t2["col2"], c = t2["col3"] }
                into joinedComboTable
            select joinedComboTable).ToList();

        //Result
        var newTable = results.FirstOrDefault()?.CopyToDataTable();

        //However to get col4 form table 2 you need to do this
        var result2 = (
            from t1 in table1.AsEnumerable()
            join t2 in table2.AsEnumerable() on
                new { a = t1["col1"], b = t1["col2"], c = t1["col3"] } equals
                new { a = t2["col1"], b = t2["col2"], c = t2["col3"] }
            select new { a = t1["col1"], b = t1["col2"], c = t1["col3"], d = t1["col4"], e = t2["col4"] });

        //Result
        var newTable2 = table1.Clone();
        newTable2.Columns.Add("col4FromTable2", typeof(string));

        foreach (var x1 in result2)
        {
            var r = newTable2.NewRow();
            r["col1"] = x1.a;
            r["col2"] = x1.b;
            r["col3"] = x1.c;
            r["col4"] = x1.d;
            r["col4FromTable2"] = x1.e;
            newTable2.Rows.Add(r);
        }
    }

您可以通過調用var rows = result.FirstOrDefault()返回一個 IEnumerable 行來獲得第一級。 創建數據表的新實例var newtable = new DataTable(); 然后循環將行添加到新數據表的行集合屬性中,就像這樣......

for(var i=0; i <= rows.Count(); i++)
     newtable.Rows.Add(rows[i]);

這樣的事情應該適合你。 抱歉打錯了..我正在使用我的手機

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM