实体框架中的查询

时间:2011-05-28 05:34:33

标签: asp.net asp.net-mvc entity-framework

您好我已经尝试了以下代码,它只显示赋值,调用增量和新对象表达式可以作为语句使用

var res = from results in db.JobSearchAgents
where results.SiteID == 110 && results.UserID == sess
select new Agentlist
{
    JobSearchAgentID = results.JobSearchAgentID.ToString(),
    EmailAddress = results.EmailAddress,
    Keywords = results.Keywords,
    Country = results.Country,
    zipcode =  results.ZipCode,
    miles = results.Miles.ToString(),
    IsActive=results.IsActive.ToString()

};
string country= (new  ObservableCollection<Agentlist>(res))[0].Country);

请给我解决方案

1 个答案:

答案 0 :(得分:1)

如果您希望查询中有多个结果,请尝试以下方法:

// Calling ToList will force query execution.
List<Agentlist> result = res.ToList<Agentlist>();

// Calling First will take the first item in the collection.
// If there are more than one item it will NOT throw 
// an exception (calling Single() will).
Agentlist agent = result.First<Agentlist>();

string country = agent.Country;

如果您只需要查询的第一个结果,可以立即尝试拨打First()

Agentlist agent = res.First<Agentlist>();