我试图通过我的MVC控制器中的POST请求将字符串变量传递给LINQ查询。我已经尝试了动态LINQ示例的许多变体(这似乎是主要的推荐方法。
IEnumerable数组,其中包含基于ng-repeat中显示的指定SiteID的结果。
public IEnumerable<SpecialsViewModel> GetAllSpecialsBySiteID(string siteID)
{
var query = (from s in _calcContext.Specials
where s.SiteID == siteID
select new SpecialsViewModel
{
ID = s.ID,
SiteID = s.SiteID,
Title = s.Title,
Description = s.Description,
Price = s.Price,
Position = s.Position,
EffectiveDate = s.EffectiveDate,
CreationDate = s.CreationDate,
Status = s.Status,
LastUpdated = s.LastUpdated
}).ToList();
return query;
}
RESULT
没有返回任何结果。
public IEnumerable<SpecialsViewModel> GetAllSpecialsBySiteID(string siteID)
{
var query = (from s in _calcContext.Specials
select new SpecialsViewModel
{
ID = s.ID,
SiteID = s.SiteID,
Title = s.Title,
Description = s.Description,
Price = s.Price,
Position = s.Position,
EffectiveDate = s.EffectiveDate,
CreationDate = s.CreationDate,
Status = s.Status,
LastUpdated = s.LastUpdated
});
query = query.Where("SiteID=@0", siteID);
return query;
}
RESULT
没有返回任何结果。
答案 0 :(得分:0)
如果你的两个linq查询都无法返回结果,我怀疑你传入的值不是你所期望的。尝试将siteID的值硬编码为已知的良好值,并查看是否有效。 (我更喜欢第一个查询,但两者都很好)
答案 1 :(得分:0)
尝试将siteID
转换为int并传递给where子句
例如:where s.SiteID == (int32?)siteID