C# 如何从方法返回泛型列表
我想从方法返回一个未知类型的列表。代码的“返回”行出现错误C# 如何从方法返回泛型列表,c#,.net,C#,.net,我想从方法返回一个未知类型的列表。代码的“返回”行出现错误 public List<T> GetMethod<T>() { var categoryCounts = (from r in reqCount group r by new { r.First, r.Second
public List<T> GetMethod<T>()
{
var categoryCounts =
(from r in reqCount
group r by new
{
r.First,
r.Second
}
into g
select new
{
MonthNumber = g.Key.First,
Category = g.Key.Second,
OrdersCount = g.Count()
}).OrderBy(m => m.MonthNumber).ThenBy(c => c.Category).ToList();
return categoryCounts;
}
public List GetMethod()
{
var分类计数=
(从r开始,在reqCount中)
新成立的r组
{
r、 首先,,
r、 第二
}
进入g
选择新的
{
MonthNumber=g.Key.First,
类别=g.Key.Second,
OrderScont=g.Count()
}).OrderBy(m=>m.MonthNumber).ThenBy(c=>c.Category.ToList();
返回类别计数;
}
提前感谢您不能从方法返回匿名类型 您可以返回一个
列表
,但这并不是通用的
或者,您可以创建命名类型并返回包含以下内容的列表:
public class MyType
{
int MonthNumber {get;set;}
string Category {get;set;}
int OrdersCount {get;set;}
}
public List<MyType> GetMethod()
{
var categoryCounts =
(from r in reqCount
group r by new
{
r.First,
r.Second
}
into g
select new MyType()
{
MonthNumber = g.Key.First,
Category = g.Key.Second,
OrdersCount = g.Count()
}).OrderBy(m => m.MonthNumber).ThenBy(c => c.Category).ToList();
return categoryCounts;
}
公共类MyType
{
int MonthNumber{get;set;}
字符串类别{get;set;}
int ordersont{get;set;}
}
公共列表GetMethod()
{
var分类计数=
(从r开始,在reqCount中)
新成立的r组
{
r、 首先,,
r、 第二
}
进入g
选择新的MyType()
{
MonthNumber=g.Key.First,
类别=g.Key.Second,
OrderScont=g.Count()
}).OrderBy(m=>m.MonthNumber).ThenBy(c=>c.Category.ToList();
返回类别计数;
}
无法从方法返回匿名类型
您可以返回一个列表
,但这并不是通用的
或者,您可以创建命名类型并返回包含以下内容的列表:
public class MyType
{
int MonthNumber {get;set;}
string Category {get;set;}
int OrdersCount {get;set;}
}
public List<MyType> GetMethod()
{
var categoryCounts =
(from r in reqCount
group r by new
{
r.First,
r.Second
}
into g
select new MyType()
{
MonthNumber = g.Key.First,
Category = g.Key.Second,
OrdersCount = g.Count()
}).OrderBy(m => m.MonthNumber).ThenBy(c => c.Category).ToList();
return categoryCounts;
}
公共类MyType
{
int MonthNumber{get;set;}
字符串类别{get;set;}
int ordersont{get;set;}
}
公共列表GetMethod()
{
var分类计数=
(从r开始,在reqCount中)
新成立的r组
{
r、 首先,,
r、 第二
}
进入g
选择新的MyType()
{
MonthNumber=g.Key.First,
类别=g.Key.Second,
OrderScont=g.Count()
}).OrderBy(m=>m.MonthNumber).ThenBy(c=>c.Category.ToList();
返回类别计数;
}
无法从方法返回匿名类型
您可以返回一个列表
,但这并不是通用的
或者,您可以创建命名类型并返回包含以下内容的列表:
public class MyType
{
int MonthNumber {get;set;}
string Category {get;set;}
int OrdersCount {get;set;}
}
public List<MyType> GetMethod()
{
var categoryCounts =
(from r in reqCount
group r by new
{
r.First,
r.Second
}
into g
select new MyType()
{
MonthNumber = g.Key.First,
Category = g.Key.Second,
OrdersCount = g.Count()
}).OrderBy(m => m.MonthNumber).ThenBy(c => c.Category).ToList();
return categoryCounts;
}
公共类MyType
{
int MonthNumber{get;set;}
字符串类别{get;set;}
int ordersont{get;set;}
}
公共列表GetMethod()
{
var分类计数=
(从r开始,在reqCount中)
新成立的r组
{
r、 首先,,
r、 第二
}
进入g
选择新的MyType()
{
MonthNumber=g.Key.First,
类别=g.Key.Second,
OrderScont=g.Count()
}).OrderBy(m=>m.MonthNumber).ThenBy(c=>c.Category.ToList();
返回类别计数;
}
无法从方法返回匿名类型
您可以返回一个列表
,但这并不是通用的
或者,您可以创建命名类型并返回包含以下内容的列表:
public class MyType
{
int MonthNumber {get;set;}
string Category {get;set;}
int OrdersCount {get;set;}
}
public List<MyType> GetMethod()
{
var categoryCounts =
(from r in reqCount
group r by new
{
r.First,
r.Second
}
into g
select new MyType()
{
MonthNumber = g.Key.First,
Category = g.Key.Second,
OrdersCount = g.Count()
}).OrderBy(m => m.MonthNumber).ThenBy(c => c.Category).ToList();
return categoryCounts;
}
公共类MyType
{
int MonthNumber{get;set;}
字符串类别{get;set;}
int ordersont{get;set;}
}
公共列表GetMethod()
{
var分类计数=
(从r开始,在reqCount中)
新成立的r组
{
r、 首先,,
r、 第二
}
进入g
选择新的MyType()
{
MonthNumber=g.Key.First,
类别=g.Key.Second,
OrderScont=g.Count()
}).OrderBy(m=>m.MonthNumber).ThenBy(c=>c.Category.ToList();
返回类别计数;
}
方法签名返回泛型类型,但其中的代码返回特定匿名类型的列表。编译器无法将匿名类型强制转换为调用方使用的任何t
。(GetMethod在您的实现中没有意义)
在不知道如何使用它的情况下,不清楚您的方法是泛型还是返回具体类型。您的方法签名返回泛型类型,但其中的代码返回特定匿名类型的列表。编译器无法将匿名类型强制转换为调用方使用的任何
t
。(GetMethod在您的实现中没有意义)
在不知道如何使用它的情况下,不清楚您的方法是泛型还是返回具体类型。您的方法签名返回泛型类型,但其中的代码返回特定匿名类型的列表。编译器无法将匿名类型强制转换为调用方使用的任何
t
。(GetMethod在您的实现中没有意义)
在不知道如何使用它的情况下,不清楚您的方法是泛型还是返回具体类型。您的方法签名返回泛型类型,但其中的代码返回特定匿名类型的列表。编译器无法将匿名类型强制转换为调用方使用的任何
t
。(GetMethod在您的实现中没有意义)
在不知道如何使用它的情况下,不清楚您的方法是否应该是泛型的,或者是否应该返回具体类型。您可以将返回类型设置为动态的
。不过,对于这种返回类型,您需要非常小心。您丢失了编译时检查,intellisense将不再适用于返回对象。您可以
public List<object> GetMethod(){}