Linq 嵌套 select new 不起作用

发布于 2024-08-01 15:15:47 字数 1731 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒与心事 2024-08-08 15:15:47

“UserModel.Company 始终为空。”

由于您使用以 .SingleOrDefault() 结尾的表达式来设置此值,因此我建议查询不会返回单个项目。 在那里开始调查。 如果您期望 u.Companies 中只有一项,请更改为 .Single() 并强制提前失败。

我认为,您可以在创建新的 CompanyModel 对象之前执行 .Single()

至于风格,我喜欢查询理解语法(“from x in y select”),但与传统的点符号语法结合使用时发现它很尴尬。 只是很难读。 (LINQ - 流畅的查询表达式 -其中一种相对于另一种有什么好处吗?)。

考虑在查询理解中使用 let 使其更清晰。

另外,由于查询已经返回 IEnumerable,并且调用 ToList() 会强制实现所有项目,因此我将修改我的方法以返回 IEnumerable< ;T> 如果可能的话。

因此,就您的情况而言,我会重构第一句话:

public IEnumerable<User> GetUsers()
{ 
    return from u in SubsonicSqlServer.Users.All()
        let c = u.Companies.Single()
        select new UserModel
        {
            UserId = u.UserId,
            Company = new CompanyModel
            {
                CompanyId = c.CompanyId,
                CompanyName = c.CompanyName
            },
            FirstName = e.FirstName,
            LastName = e.LastName,
            BirthDate = e.BirthDate
        };
}

如果它在您的对象模型中有意义,您可以修改 User 以获得采用任何类型 u 的构造函数是,而且它变得更简单:

return from u in SubsonicSqlServer.Users.All()
    select new UserModel (u);

或者甚至

return SubsonicSqlServer.Users.All().Select(u => new UserModel (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 in u.Companies, change to .Single() and force an early failure.

You can do the .Single() before creating the new CompanyModel 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 calling ToList() forces all items to be realized, I would modify my method to return IEnumerable<T> if possible.

So, in your case, I would refactor the first to say:

public IEnumerable<User> GetUsers()
{ 
    return from u in SubsonicSqlServer.Users.All()
        let c = u.Companies.Single()
        select new UserModel
        {
            UserId = u.UserId,
            Company = new CompanyModel
            {
                CompanyId = c.CompanyId,
                CompanyName = c.CompanyName
            },
            FirstName = e.FirstName,
            LastName = e.LastName,
            BirthDate = e.BirthDate
        };
}

If it makes sense in your object model, you could modify User to have a constructor that takes whatever type u is, and it gets even simpler:

return from u in SubsonicSqlServer.Users.All()
    select new UserModel (u);

or even

return SubsonicSqlServer.Users.All().Select(u => new UserModel (u));
知足的幸福 2024-08-08 15:15:47

两件事

  1. 当您的方法的签名行显示 IList 时,您将返回 ListUserModel 是否继承自 < code>User?

  2. 我是否遗漏了什么,e 从哪里来?

名字 = e.名字,
姓氏 = e.姓氏,
出生日期 = e.出生日期引用

Two things

  1. You're returning a List<UserModel> when your method's signature line says IList<User> does UserModel inherit from User?

  2. Am I missing something, where does e come from?

FirstName = e.FirstName,
LastName = e.LastName,
BirthDate = e.BirthDate Blockquote

甜心小果奶 2024-08-08 15:15:47

请查看我的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文