如何使用 LINQ 检查空值

发布于 2024-08-15 12:24:57 字数 469 浏览 3 评论 0原文

我有这个代码。如何使用 SingleOrDefault 方法检查空值?

public static List<ETY.Rol> GetRolesByApplicationAndCompany(this UsuarioContext usuario, int company, int app)
        {
            List<ETY.Company> lCompanies= usuario.Companies;

            var roles = lCompanies.
                SingleOrDefault(e => (e.Id == company)).Applications.
                SingleOrDefault(a => a.Id == app).Roles;
            return roles;

        }

I have this code. How can I check for null values with the SingleOrDefault method?

public static List<ETY.Rol> GetRolesByApplicationAndCompany(this UsuarioContext usuario, int company, int app)
        {
            List<ETY.Company> lCompanies= usuario.Companies;

            var roles = lCompanies.
                SingleOrDefault(e => (e.Id == company)).Applications.
                SingleOrDefault(a => a.Id == app).Roles;
            return roles;

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

神爱温柔 2024-08-22 12:24:57

您可以尝试查看 Maybe/IfNotNull 扩展方法(此处此处)。

或者使用类似这样的 Linq 语法(未经测试):

var q = from company in lCompanies.SingleOrDefault(e => e.Id == company)
        where company != null
        let application = company.Applications.SingleOrDefault(a => a.Id == app)
        where application != null
        select application.Roles;

(Greg Beech 的答案 是如果单身条件得到保证更好)

You could try looking at a Maybe/IfNotNull extension method (here and here).

Or use Linq syntax something like this (untested):

var q = from company in lCompanies.SingleOrDefault(e => e.Id == company)
        where company != null
        let application = company.Applications.SingleOrDefault(a => a.Id == app)
        where application != null
        select application.Roles;

(Greg Beech's answer is better if the Single condition is guaranteed)

长安忆 2024-08-22 12:24:57

您的意思是如果任何 SingleOrDefault 返回 null 则返回 null 或空列表?在这种情况下:

var company = lCompanies.SingleOrDefault(e => (e.Id == company));
if(company != null) {
    var application = company.Applications.SingleOrDefault(a => a.Id == app);
    if(application!=null) {
        return application.Roles;
    }
}
return null; //Or: return new List<ETY.Rol>();

Do you mean returning null or an empty list if any of the SingleOrDefault returns null? In that case:

var company = lCompanies.SingleOrDefault(e => (e.Id == company));
if(company != null) {
    var application = company.Applications.SingleOrDefault(a => a.Id == app);
    if(application!=null) {
        return application.Roles;
    }
}
return null; //Or: return new List<ETY.Rol>();
夜夜流光相皎洁 2024-08-22 12:24:57

您可以编写一个链式查询,如下所示,而不是使用 SingleOrDefault。您失去了确保只有一个应用程序或公司的语义,但如果您知道情况总是如此,那么这应该不是问题。

return from c in lCompanies where c.Id == company
       from a in c.Applications where a.Id == app
       select a.Roles;

Rather than using SingleOrDefault you could write a chained query as follows. You lose the semantics of ensuring that there's only a single application or company, but if you know that to always be the case then it shouldn't be a problem.

return from c in lCompanies where c.Id == company
       from a in c.Applications where a.Id == app
       select a.Roles;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文