Skip to main content
 首页 » 编程设计

c#之使用泛型类型 System.Collections.Generic.ListT> 需要 1 个类型参数

2024年02月13日11kerrycode

我将我的应用程序 .Net Framework 版本从 3.5 升级到 4.5.1,并将目标版本从 3.5 更改为 4.5.1。我的应用程序使用 Linq to SQL。在我的应用程序中,我使用类库,在类库中,我需要在通用列表中传递多个参数...

在数据访问层中,我有方法试图将 Linq 生成的多个类传递给 Sql。 (tblaccount, vendorCategory)

internal class BasicDataDAO 
{ 
 
  internal List<tblaccount, vendorCategory> GetAllvendor() 
    { 
       using (CommonDataContext VendorDC = new CommonDataContext(Settings.ConnectionString)) 
         { 
             VendorDC.DeferredLoadingEnabled = true; 
 
             return (from V in VendorDC.tblaccounts 
                    join C in VendorDC.vendorCategories on V.CatID equals C.Vendor_CatID 
                            where V.Sys_AC_type == 7 
                            select new 
                            { 
                                V.ID, 
                                V.ACCOUNT, 
                                V.field2, 
                                V.contactDetails, 
                                V.Remarks, 
                                V.field3, 
                                V.field1, 
                                V.email, 
                                V.fax, 
                                C.vendor_CategoryName 
                            }).ToList();            
                } 
            } 
} 

我是如何在业务层处理这个问题的

public List<tblaccount, vendorCategory> GetAllVendors()        
{ 
            BasicDataDAO dao = new BasicDataDAO(); 
            return dao.GetAllvendor();             
} 

但是我在数据层和业务层都遇到了这个错误

Using the generic type System.Collections.Generic.List requires 1 type arguments

如何实现这个任务,你可以在Generic List中传递多个linq生成的类?

虽然我已经将 .Net Framework 从 3.5 升级到 4.5.1,但我仍然无法在我的类库中引用 System.Tuple 类

请您参考如下方法:

您不能声明带有两个类型参数的列表。

您可以使用 Tuple<T1, T2>相反:List<Tuple<tblaccount, vendorCategory>>或使用 2 个属性创建您自己的类。

但是查看您的代码,您似乎正试图将匿名类型实例作为方法结果返回。这不是匿名类型的用途:

You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

from Anonymous Types (C# Programming Guide)

遵循指南并创建您自己的类来保存查询结果,并将方法声明为 List<MyClass> .