使用Iqueryable从数据库返回一个列表?

时间:2017-03-08 20:20:41

标签: c# asp.net-mvc linq-to-sql

我尝试使用IQueryable从数据库中返回项目。我不确定如何实现这一点。我试图返回db.Phones.Where(来自someQueryable中的x);我的错误是错误:无法找到源类型' Iquerable

的查询模式的实现
private IList<Phone> GetPhones(int carrierId, string emailaddress)
{
    string pattern = @"^([a-zA-Z0-9._%+-]{1,50})(@gmail|@GMail)(\.)(com|COM)$"; //regular expression
    Match match = Regex.Match(emailaddress, pattern);//matches users email domain address with regular expression

    IQueryable someQueryable = null; //need help
    if (match.Success) // if true it will do the following
    {
        someQueryable = from g in db.Phones //linq to sql
            where g.GovAllowed == true
            select g;
    }
    else
    {
        someQueryable = from c in db.Phones // linq to sql
            where c.CommerceAllowed == true
            select c;
    }

    return db.Phones.Where( from x in someQueryable);//need help on this to return the data from database whether true or false           
}

1 个答案:

答案 0 :(得分:3)

我猜你在这里遇到语法错误? - &GT;

return db.Phones.Where( from x in someQueryable);

试试这个..

IQueryable<Phone> someQueryable = null;

    if (match.Success)
    {
        someQueryable = from g in db.Phones
            where g.GovAllowed == true
            select g;
    }
    else
    {
        someQueryable = from c in db.Phones
            where c.CommerceAllowed == true
            select c;
    }

   return someQueryable.ToList();