Asp.net 实体框架中的查询

Asp.net 实体框架中的查询,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,返回视图之前,必须在JobSearchAgentID上应用if条件 var res = (from results in db.JobSearchAgents where results.SiteID == 110 && results.UserID == sess select results).AsEnumerable().Select(results =>

返回视图之前,必须在JobSearchAgentID上应用if条件

var res = (from results in db.JobSearchAgents
                            where results.SiteID == 110 && results.UserID == sess
                            select results).AsEnumerable().Select(results => new Agentlist
                            {
                                JobSearchAgentID = results.JobSearchAgentID.ToString(),

                                EmailAddress = results.EmailAddress,
                                Keywords = results.Keywords,


                                Country = results.Country,

                                zipcode = results.ZipCode,
                                miles = results.Miles.ToString()

                            });

            return View(res);

您可以使用内联条件

 (from results in db.JobSearchAgents
                            where results.SiteID == 110 && results.UserID == sess
                            select results).AsEnumerable().Select(results => new Agentlist
                            {
                               //it is just an example
                                JobSearchAgentID =JobSearchAgentID!=null? results.JobSearchAgentID.ToString():[somthing else],

                                EmailAddress = results.EmailAddress,
                                Keywords = results.Keywords,

                                //another example
                                Country = String.IsNullOrEmpty(results.Country)?"No Country":results.Country,
                                //the last example
                                zipcode =results.Country=="United States"? "123": results.ZipCode,
                                miles = results.Miles.ToString()

                            });

            return View(res);

查询可以重写为:

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.Country=="United States" ? "123" : results.ZipCode,
    miles = results.Miles.ToString()
});
string country = string.Emtpy;

foreach (var agentLst in res)
{
  country = agentLst.Country;     
}
我的最后一个答案是在MVVM的上下文中,我们通常使用ObservableCollection(存在于System.Collections.ObjectModel命名空间中)。无论如何,我已经更新了我的答案。这将在您的场景中起作用。
由于上面linq查询的结果是一个集合(IEnumerable),因此您需要循环遍历它以获得所需的数据。希望它能回答你的问题

你的问题是什么?我必须检查res.country==“”然后我可以做什么我必须检查res.country==“uinited states”并检查它是否等于美国然后res.zipcode=“123”你可以修改条件。YourField=[谓词]?[then]:[else]我必须执行类似以下字符串的操作country=res.countryit缺少程序集引用It正在显示类型或命名空间observable collection找不到它正在显示错误仅赋值、调用增量、减量、新对象表达式可用作语句