[Linq-To-Sql]如何返回两个不同数据类型的值?

发布于 2024-09-01 14:51:11 字数 854 浏览 9 评论 0原文

这是我的存储库方法,它返回 UserId

public IQueryable<int> getLoginStatus(string emailId, string password)
{
    return (from r in taxidb.Registrations
           where (r.EmailId == emailId && r.Password == password)
           select r.UserId);
}

如何返回 UserName (它是一个字符串以及 UserId)...有什么建议吗?

编辑:

我尝试过它有效,但是如何获取查询结果是否包含记录,

    public RegistrationBO getLoginStatus(string emailId, string password)
    {
        return (from r in taxidb.Registrations
                where (r.EmailId == emailId && r.Password == password)
                select new RegistrationBO()
                {
                    UserId = r.UserId,
                    UserName = r.UserName
                }).FirstOrDefault();
    }

Here is my repository method which returns UserId ,

public IQueryable<int> getLoginStatus(string emailId, string password)
{
    return (from r in taxidb.Registrations
           where (r.EmailId == emailId && r.Password == password)
           select r.UserId);
}

How to return UserName which is a string along with UserId... Any suggestion?

EDIT:

I tried this it is working but how to get whether the query result contains records or not,

    public RegistrationBO getLoginStatus(string emailId, string password)
    {
        return (from r in taxidb.Registrations
                where (r.EmailId == emailId && r.Password == password)
                select new RegistrationBO()
                {
                    UserId = r.UserId,
                    UserName = r.UserName
                }).FirstOrDefault();
    }

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

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

发布评论

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

评论(2

缘字诀 2024-09-08 14:51:12

您需要定义一个类来包含结果,然后使用它:

return (from r in taxidb.Registrations
       where (r.EmailId == emailId && r.Password == password)
       select new SomeClass { r.UserId, r.UserName });

虽然看起来毫无意义... SLaks 是正确的,您可以只返回用户:

return (from r in taxidb.Registrations
       where (r.EmailId == emailId && r.Password == password)
       select r);

You need to define a class to contain the result then use this:

return (from r in taxidb.Registrations
       where (r.EmailId == emailId && r.Password == password)
       select new SomeClass { r.UserId, r.UserName });

Seems pointless though... SLaks is right that you can just return the user:

return (from r in taxidb.Registrations
       where (r.EmailId == emailId && r.Password == password)
       select r);
岁月静好 2024-09-08 14:51:12

您应该返回整个 User 对象,方法是将 select 子句更改为 select r 并将返回类型更改为 IQueryable

You should return the entire User objects, by change the select clause to select r and changing the return type to IQueryable<User>.

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