Linq 嵌套 select new 不起作用
我正在尝试使用 Subsonic 进行急切加载,但它一直为我返回 null 。
在下面的方法中,我尝试水合包含另一个域模型(CompanyModel)的域模型(UserModel)。 但是,使用下面的代码,UserModel.Company 始终为 null。
我在这里缺少什么。 任何帮助,将不胜感激。
public IList<UserModel> GetUsers()
{
return (from u in SubsonicSqlServer.Users.All()
select new UserModel
{
UserId= u.UserId,
Company = (from c in u.Companies
select new CompanyModel
{
CompanyId = c.CompanyId,
CompanyName = c.CompanyName
}).SingleOrDefault(),
FirstName = u.FirstName,
LastName = u.LastName,
BirthDate = u.BirthDate
}).ToList();
}
更新(08/11/09):
更多地研究代码,我发现在下面的示例中设置 CompanyId 也不起作用。 我最初认为这是 Subsonic 的问题,但如果下面的代码不起作用,我猜测它与我的 Linq 语句有关。 有任何想法吗?
public IList<UserModel> GetUsers()
{
return (from u in SubsonicSqlServer.Users.All()
select new UserModel
{
UserId= u.UserId,
CompanyId = Guid.NewGuid(),
FirstName = u.FirstName,
LastName = u.LastName,
BirthDate = u.BirthDate
}).ToList();
}
更新(11/17/2009):
仍然没有找到解决方案。 但我们正在切换到 nHibernate(不是因为这个问题)。
I'm trying to get eager loading working with Subsonic, and it's been returning null for me.
In the method below, I'm trying to hydrate a domain model (UserModel) which contains another domain model (CompanyModel). However, with the code below, UserModel.Company is always null.
What am I missing here. Any help would be appreciated.
public IList<UserModel> GetUsers()
{
return (from u in SubsonicSqlServer.Users.All()
select new UserModel
{
UserId= u.UserId,
Company = (from c in u.Companies
select new CompanyModel
{
CompanyId = c.CompanyId,
CompanyName = c.CompanyName
}).SingleOrDefault(),
FirstName = u.FirstName,
LastName = u.LastName,
BirthDate = u.BirthDate
}).ToList();
}
Update (08/11/09):
More toying around with the code, I found out that setting CompanyId in the following example doesn't work either. I initially thought this was an issue with Subsonic, but if the code below doesn't work, I'm guessing it has something to do with my Linq statement. Any ideas?
public IList<UserModel> GetUsers()
{
return (from u in SubsonicSqlServer.Users.All()
select new UserModel
{
UserId= u.UserId,
CompanyId = Guid.NewGuid(),
FirstName = u.FirstName,
LastName = u.LastName,
BirthDate = u.BirthDate
}).ToList();
}
Update (11/17/2009):
Still haven't found a solution. But we are switching to nHibernate (not because of this issue).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“UserModel.Company 始终为空。”
由于您使用以
.SingleOrDefault()
结尾的表达式来设置此值,因此我建议查询不会返回单个项目。 在那里开始调查。 如果您期望u.Companies
中只有一项,请更改为.Single()
并强制提前失败。我认为,您可以在创建新的
CompanyModel
对象之前执行.Single()
。至于风格,我喜欢查询理解语法(“
from x in y select
”),但与传统的点符号语法结合使用时发现它很尴尬。 只是很难读。 (LINQ - 流畅的查询表达式 -其中一种相对于另一种有什么好处吗?)。考虑在查询理解中使用
let
使其更清晰。另外,由于查询已经返回
IEnumerable
,并且调用ToList()
会强制实现所有项目,因此我将修改我的方法以返回IEnumerable< ;T>
如果可能的话。因此,就您的情况而言,我会重构第一句话:
如果它在您的对象模型中有意义,您可以修改
User
以获得采用任何类型u
的构造函数是,而且它变得更简单:或者甚至
"UserModel.Company is always null."
since you are setting this with an expression that ends with
.SingleOrDefault()
, I'm going to suggest that the query isn't returning a single item. Start investigating there. If you are expecting exactly one item inu.Companies
, change to.Single()
and force an early failure.You can do the
.Single()
before creating the newCompanyModel
object, I think.As for style, I like the query comprehension syntax ("
from x in y select
") but find it awkward when combined with traditional dot-notation syntax. It's just hard to read. (LINQ - Fluent and Query Expression - Is there any benefit(s) of one over other?).Consider using
let
in the query comprehension to make it clearer.Also, since a query already returns an
IEnumerable<T>
, and callingToList()
forces all items to be realized, I would modify my method to returnIEnumerable<T>
if possible.So, in your case, I would refactor the first to say:
If it makes sense in your object model, you could modify
User
to have a constructor that takes whatever typeu
is, and it gets even simpler:or even
两件事
当您的方法的签名行显示
IList
时,您将返回List
,UserModel
是否继承自 < code>User?我是否遗漏了什么,
e
从哪里来?Two things
You're returning a
List<UserModel>
when your method's signature line saysIList<User>
doesUserModel
inherit fromUser
?Am I missing something, where does
e
come from?请查看我的fork @ github (http://github.com/funky81/SubSonic- 3.0/commit/aa7a9c1b564b2667db7fbd41e09ab72f5d58dcdb)对于这个解决方案,实际上当亚音速尝试投影新类型类时存在问题,所以你的代码实际上没有任何问题:D
Please check out my fork @ github (http://github.com/funky81/SubSonic-3.0/commit/aa7a9c1b564b2667db7fbd41e09ab72f5d58dcdb) for this solution, actually there's a problem when subsonic try to project new type class, so there's nothin wrong with your code actually :D