我正在尝试从一个表到另一个表访问一个值。我有一个包含PainCategoryId的表PainCategories和一个包含PainCategoryId的表Triage。我试图通过Triage运行查询,以将该Id值与PainCategory表中的值进行比较。分类表包含多个行,其PainCategoryId为1,PainCategory表中只有一个PainCategoryId = 1。如果有人能提供帮助,我们将不胜感激。
这是我控制器中的linq:
tt.painCats = db.PainCategories.ToList().Where(p => p.CompanyId == CompanyId);
tt.triages = db.Triages.ToList().Where(t => t.PainCategoryId == tt.painCats.
Contains(t.PainCategoryId)); //Error is in this line because it is just not done
//right with the Contains.
分类模型:
public class Triage
{
public int Id { get; set; }
public string Status { get; set; }
public int PainCategoryId { get; set; }
public virtual PainCategory PainCategory { get; set; }
}
PainCategory模型:
public class PainCategory
{
public PainCategory()
{
Triages = new HashSet<Triage>();
}
public int PainCategoryId { get; set; }
public string Title { get; set; }
public virtual ICollection<Triage> Triages { get; set; }
}
查看剃须刀电话:
foreach (var item in Model.painCats)
{
@Html.Label(item.Title)<br/>
foreach(var tri in Model.triages)
{
@Html.RadioButton(tri.PainCategory.Title, tri.Order)
@Html.Label(tri.Description)<br/>
}
}
答案 0 :(得分:2)
肖恩,
你能试试吗
// This doesn't work because db.Triages is not List<T>,
// the Contains method only applies to Lists
tt.triages = db.Triages.ToList().Where(t => tt.painCats.Contains(t.PainCategoryId));
更新
尝试使用Any运算符,如下所示:
// The Any method is a core linq method and is implemented for both Enumerable and Queryable
tt.triages = db.Triages.ToList().Where(t => tt.painCats.Any(cat => t.PainCategoryId == cat.PainCategoryId));