优化算法以获得不同的价值观

时间:2011-04-23 10:06:57

标签: c# .net linq linq-to-sql

我有以下数据集,我想选择不同的行。因为数据是平的。 我怎么能用linq做到这一点?

Following is the sample of the data

以下是我满足我的要求的代码段,我该如何优化它?

 VendorInvoiceStagingTable = new Program().ReadExcelFile(@"C:\Users\huzaifa.gain\Desktop\Vendor invoice import - sample data set.xlsx", "Sheet2");
        var InvoiceHeadercollection = VendorInvoiceStagingTable.AsEnumerable().Select(t=>t.Field<string>(VendInvoice.Number)).Distinct();

        VendorInvoiceTable =  new Program().CreateHeader();
        foreach (var InvoiceHeader in InvoiceHeadercollection)
        {
            IEnumerable<DataRow> query = from vendInv in VendorInvoiceStagingTable.AsEnumerable()
                                         where vendInv.Field<string>(VendInvoice.Number) == InvoiceHeader
                                         select vendInv;

            Object[] obj = new Object[10];
            var item = query.First();

            for (int idx = 0; idx < 10; idx++)
            {

                obj[idx] = item[idx];

            }

           VendorInvoiceTable.Rows.Add(obj);
        }

1 个答案:

答案 0 :(得分:0)

不确定你想要什么,但也许这可以帮到你。

    var l = VendorInvoiceStagingTable.AsEnumerable().ToLookup(t => t.Field<string>(VendInvoice.Number));
    foreach (var item in l)
    {
        var itemToAdd = item.First();
        //Do add here
    }