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;
}
精彩评论