0

如何从数据表中选择所有字段?前任。:

var common = from c in dt1.AsEnumerable()
             join x in dt2.AsEnumerable() on c.Field<int>("ID") equals
             x.Field<int>("ID")
             select
             new object[]
             {
                 c["Col1"], x["NameCol1"], x["NameCol2"], x["NameCol3"], x["NameCol4"]

             };

如何选择 x 的所有字段?(如果 x 有 20 列,我不想为所有字段写名称)

4

1 回答 1

0

也许这有帮助:

        var customers = new DataTable();
        customers.Columns.Add("ID", typeof(int));
        customers.Columns.Add("Name");

        customers.Rows.Add(1, "Tom");
        customers.Rows.Add(2, "Dick");
        customers.Rows.Add(3, "Harry");

        var purchases = new DataTable();
        purchases.Columns.Add("ID");
        purchases.Columns.Add("Description");
        purchases.Columns.Add("PurchaseID", typeof(int));

        purchases.Rows.Add(1, "Bike", 1);
        purchases.Rows.Add(2, "Holiday", 1);
        purchases.Rows.Add(3, "Bike", 2);
        purchases.Rows.Add(4, "Car", 3);

        var query = customers.AsEnumerable().Join(purchases.AsEnumerable(),
                                    cust => cust.Field<int>("ID"),
                                    purch => purch.Field<int>("PurchaseID"),
                                    (cust, purch) =>
                                    {
                                        var res = new List<object>();
                                        res.AddRange(cust.ItemArray);
                                        res.AddRange(purch.ItemArray);
                                        return res.ToArray();
                                    }
                                    );
于 2012-05-16T12:57:14.427 回答