IEnumerable ;使用匿名方法

发布于 2024-11-02 17:39:40 字数 713 浏览 1 评论 0 原文

我不知道如何摆脱这个错误?

错误 1 ​​使用通用类型“System.Collections.Generic.IEnumerable”需要 1 个类型参数 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;
      }

在此输入图像描述

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;
      }

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2024-11-09 17:39:40

您的 Linq 查询返回匿名类型的序列,但方法不能返回匿名类型。您有多种选择:

  • 返回一个 IEnumerable>

    private static IEnumerable>>不同发票编号(数据表供应商发票暂存表)
    {
        var InvoiceLinecollection = VendorInvoiceStagingTable
                                        .AsEnumerable()
                                        .Select(t => Tuple.Create(t.Field(VendInvoice.Number), t.Field(VendInvoice.LineNumber)))
                                         。清楚的();
        返回 InvoiceLine 集合;
    }
    
  • , string>>

    按照另一个答案中的建议返回一个 IDictionary (假设您的查询不返回重复的键)

    private static IDictionary;不同发票编号(数据表供应商发票暂存表)
    {
        var InvoiceLinecollection = VendorInvoiceStagingTable
                                        .AsEnumerable()
                                        .Select(t => Tuple.Create(t.Field(VendInvoice.Number), t.Field(VendInvoice.LineNumber)))
                                        。清楚的()
                                        .ToDictionary(t => t.Item1, t => t.Item2);
        返回 InvoiceLine 集合;
    }
    
  • 为此目的创建一个类 2字符串属性并返回该类的序列

    private static IEnumerable;不同发票编号(数据表供应商发票暂存表)
    {
        var InvoiceLinecollection = VendorInvoiceStagingTable
                                        .AsEnumerable()
                                        .Select(t => new InvoiceLine(t.Field(VendInvoice.Number), t.Field(VendInvoice.LineNumber)))
                                        。清楚的();
        返回 InvoiceLine 集合;
    }
    

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;
    }
    
清风疏影 2024-11-09 17:39:40

为什么不使用 Dictionary 来代替?

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;
          }

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;
          }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文