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

时间:2010-05-18 13:12:49

标签: linq-to-sql string int iqueryable

这是我的存储库方法,它返回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();
    }

2 个答案:

答案 0 :(得分:3)

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

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);

答案 1 :(得分:1)

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