Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表的返回类型_C#_Linq_Anonymous Types - Fatal编程技术网

C# 列表的返回类型

C# 列表的返回类型,c#,linq,anonymous-types,C#,Linq,Anonymous Types,您好,我需要找到一种方法来声明方法的匿名类型。这是我的代码: public List<var> ListOfProducts(string subcategory) { var products = (from p in dataContext.Products join s in dataContext.SubCategories.Where(x => x.SubCatName == subcategory)

您好,我需要找到一种方法来声明方法的匿名类型。这是我的代码:

public List<var> ListOfProducts(string subcategory)
{
    var products = (from p in dataContext.Products
                    join s in dataContext.SubCategories.Where(x => x.SubCatName == subcategory)
                        on p.SubcatId equals s.SubCatId
                    join b in dataContext.Brands on p.BrandId equals b.BrandId
                    select new
                    {
                        Subcategory = s.SubCatName,
                        Brand = b.BrandName,
                        p.ProductName,
                        p.ProductPrice
                    });
    return products;
} 
我不知道应该为该方法设置列表的类型。在这种情况下我应该怎么做?

您不能从方法返回匿名类型

只需为您的类型创建一个类并返回该类

public class Product
{
    string Subcategory { get; set; }
    string Brand { get; set; }
    string ProductName { get; set; }
    decimal ProductPrice { get; set; }
}
然后返回:

var products = (from p in dataContext.Products
                    join s in dataContext.SubCategories.Where(x => x.SubCatName == subcategory) on p.SubcatId
                        equals s.SubCatId
                    join b in dataContext.Brands on p.BrandId equals b.BrandId
                    select new Product
                               {
                                   Subcategory = s.SubCatName,
                                   Brand = b.BrandName,
                                   p.ProductName,
                                   p.ProductPrice
                               });

    return products;

编辑:为了澄清我的第一句话,正如@JamesMichaelHare指出的,从技术上讲,通过返回对象或动态从方法返回匿名类型是可能的,但这可能比它的价值更麻烦,因为您必须使用反射或其他方式访问对象的属性

根据MSDN,动态类型允许发生在其中的操作绕过编译时类型检查。相反,这些操作是在运行时解决的

因此,请尝试以下方法:

public IEnumerable<dynamic> ListOfProducts(string subcategory) 

我想说的是,您应该为此定义另一个模型,如果您将返回的结果用于表示层,您应该定义ViewModel,或者如果您用于分发层,您可以定义为Dto对象

public class ProductDto
{
    public string Subcategory {get; set; }
    public string Brand { get; set; }
    public string ProductName{ get; set; }
    public decimal ProductPrice{ get; set; }
}

请注意:匿名类型设计用于方法或函数的范围内,而不是方法或函数的范围外

但是有一种方法可以通过一些扩展方法和一些额外的铸件来实现,我不喜欢这样:

在代码中,还应该将.ToList添加到LINQ表达式中

扩展方法包括:

static List<T> InitList<T>(this T o) { return new List<T>(); }
static T CastToThis<T>(this  T target, object o) { return (T)o; }
现在,使用以下命令将方法的返回对象强制转换为此列表的类型:

list = list.CastToThis(returnedValueOfYourMethod);
还有一件事:匿名类型仅在程序集中有效,不能跨程序集边界传递。发件人:

C规范保证,当您在一个程序集中的两个位置使用相同的匿名类型时,这些类型将统一为一个类型。为了保持相同,这两个匿名类型必须具有完全相同的成员名称和完全相同的成员类型,顺序完全相同


总而言之,我不明白为什么需要这样做,因为声明一个新类型要实际得多,如果你真的需要,你应该研究C中的动态类型,如果你想做一些更神奇的事情,你应该使用反射。

我想你不能这样做,因为类型是方法的本地类型。声明其他人可以实际使用的公共类型。您将如何使用此方法?我在您的另一个问题中为您提供了答案-您需要使用所需的属性声明您自己的类。关于我之前的评论,您有什么不明白的地方?根据您使用该方法的方式,您可能会使用类似于此的可能重复的方法。作为澄清,您是正确的,因为您不能将方法的返回类型设置为匿名类型,但是可以从方法返回匿名对象,如object、dynamic等,尽管如果对象的作用域超出了生存期匿名类型中的方法,那么这不是最好的,而自定义类更好。你说得对,只是想确保他们不认为这意味着匿名对象本身无法返回。。。
list = list.CastToThis(returnedValueOfYourMethod);