问题描述
这是一个基本问题.我已经设置了基本的SL4/RIA项目,并且我想在域服务中创建一个新方法并从中返回一些数据.我不确定执行此操作的最简单方法.我应该将其包装在ToList()中吗?我不清楚如何处理这种创建的匿名类型..返回此数据的最简单方法是什么?
This is a basic question. I have the basic SL4/RIA project set up and I want to create a new method in the domain service and return some data from it. I am unsure the proper easiest way to do this.. Should I wrap it up in a ToList()? I am unclear how to handle this anonymous type that was create.. what is the easiest way to return this data?
public IQueryable<ApplicationLog> GetApplicationLogsGrouped()
{
var x = from c in ObjectContext.ApplicationLogs
let dt = c.LogDate
group c by new { y = dt.Value.Year, m = dt.Value.Month, d = dt.Value.Day } into mygroup
select new { aaa = mygroup.Key, ProductCount = mygroup.Count() };
return x;
// return this.ObjectContext.ApplicationLogs.Where(r => r.ApplicationID < 50);
}
无法隐式转换类型'System.Linq.IQueryable< AnonymousType#1>'到"System.Linq.IQueryable< CapRep4.Web.ApplicationLog>".存在明确的转换(您是否缺少演员表?)58 20 CapRep4.Web
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<CapRep4.Web.ApplicationLog>'. An explicit conversion exists (are you missing a cast?) 58 20 CapRep4.Web
推荐答案
匿名类型与其他任何类一样,但是由编译器创建.编译器生成的内容类似于:
An anonymous type is like any other class, but it's created by the compiler. What the compiler generates is something like:
class AnonymousType1 {
public AnonymousType2 Key { get; set; }
public int ProductCount { get; set; }
}
class AnonymousType2 {
public int y { get; set; }
public int m { get; set; }
public int d { get; set; }
}
您无法访问这些类,因此如果您要保持强类型输入,则别无选择,只能使用与Anonymous1的定义匹配的自定义类.然后,您将像这样使用它:new MyClass { Key = myGroup.Key, ProductCount = mygroup.Count() }
.
Those classes are not accessible to you so you have no choice but to use a custom class matching the definition of Anonymous1 instead if you want to keep strong typing. You'll then use it like this: new MyClass { Key = myGroup.Key, ProductCount = mygroup.Count() }
.
这篇关于匿名linq查询选择的返回类型是什么?返回此数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!