working with IQueryable; not working
I'm using linq-to-EF and I have this:
public IQeuryable<base01> GetData()
{
var u = this.ObjectContext.base01;
IQueryable<base01> u2 = u.OrderBy(o => o.article)
.Select(l => new { Id = l.Id, article = l.article, lot = l.lot}) 开发者_运维问答as IQueryable<base01>;
return u2;
}
Basically, u contains the result of a query and I'm looking to sort and rearrange the columns. When I replace return u2 with return u I get what the result set filled but whe I go for return u2 I get a null.
What's the problem that I'm not seeing?
Thanks.
as IQueryable<base01>
The problem is as - you are projecting to an anonymous type in your query not to base01 - so as will return null. Instead create new instances of base01 in your projection and remove the as operator:
IQueryable<base01> u2 = u.OrderBy(o => o.article)
.Select(l => new base01()
{ Id = l.Id, article = l.article, lot = l.lot });
Edit:
If u is already an IQueryable<base01> and base01 is one of your entities you do not need the Select() projection at all since you already have the type you want:
IQueryable<base01> u2 = u.OrderBy(o => o.article);
return u2;
You cannot cast this way, use code below.
IQueryable<base01> u2 = u.OrderBy(o => o.article)
.Select(l => new base01 { Id = l.Id, article = l.article, lot = l.lot});
加载中,请稍侯......
精彩评论