简体   繁体   中英

IEnumerable <T> with anonymous methods

I dont know how to get rid of this error ?

Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires 1 type arguments C:\\Users\\huzaifa.gain\\documents\\visual studio 2010\\Projects\\VendInvoiceImport\\VendInvoiceImport\\Program.cs 34 24 VendInvoiceImport

private static IEnumerable<string , string >  DistinctInvoiceNumber(DataTable   VendorInvoiceStagingTable)
       {
           var InvoiceLinecollection = VendorInvoiceStagingTable.AsEnumerable().Select(t => new {  number = t.Field<string>(VendInvoice.Number),LineNumber = t.Field<string>(VendInvoice.LineNumber)}).Distinct();
        return InvoiceLinecollection;
      }

在此处输入图片说明

Your Linq query returns an sequence of anonymous type, but methods can't return anonymous types. You have several options:

  • return an IEnumerable<Tuple<string, string>>

     private static IEnumerable<Tuple<string, string>> DistinctInvoiceNumber(DataTable VendorInvoiceStagingTable) { var InvoiceLinecollection = VendorInvoiceStagingTable .AsEnumerable() .Select(t => Tuple.Create(t.Field<string>(VendInvoice.Number), t.Field<string>(VendInvoice.LineNumber))) .Distinct(); return InvoiceLinecollection; } 
  • return a IDictionary<string, string> as suggested in another answer (assuming your query doesn't return duplicate keys)

     private static IDictionary<string, string> DistinctInvoiceNumber(DataTable VendorInvoiceStagingTable) { var InvoiceLinecollection = VendorInvoiceStagingTable .AsEnumerable() .Select(t => Tuple.Create(t.Field<string>(VendInvoice.Number), t.Field<string>(VendInvoice.LineNumber))) .Distinct() .ToDictionary(t => t.Item1, t => t.Item2); return InvoiceLinecollection; } 
  • create a class for this purpose with 2 string properties and return a sequence of that class

     private static IEnumerable<InvoiceLine> DistinctInvoiceNumber(DataTable VendorInvoiceStagingTable) { var InvoiceLinecollection = VendorInvoiceStagingTable .AsEnumerable() .Select(t => new InvoiceLine(t.Field<string>(VendInvoice.Number), t.Field<string>(VendInvoice.LineNumber))) .Distinct(); return InvoiceLinecollection; } 

Why you do not use Dictionary<string, string> instead?

private static Dictionary<string , string >  DistinctInvoiceNumber(DataTable   VendorInvoiceStagingTable)
       {
           var InvoiceLinecollection = VendorInvoiceStagingTable.AsEnumerable().Select(t => new {  number = t.Field<string>(VendInvoice.Number),LineNumber = 
                 t.Field<string>(VendInvoice.LineNumber)}).ToDictionary();
            return InvoiceLinecollection;
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM