// 获取类别表数据
var typeIDList = new List<int> { 2, 3, 4, 5, 6, 7 };
var typeList = db.T_Dictionary
.Where(d => typeIDList.Contains(d.TypeID))
.Select(d => new { d.TypeID, d.DKey, d.DValue }).ToList();
//获取植物的数据
var data = db.T_Plants2.Where(d => d.PlantID == id).Select(p => new Plants
{
PlantID=p.PlantID,
Family=p.Family,
//根据id,获取类别名称
//OriginName = (typeList.Where(d => d.TypeID == 2 && d.DKey == p.Origin).Select(d => d.DValue).FirstOrDefault()),//报错
}).FirstOrDefault();
正确做法
if (data != null)
{
data.OriginName = typeList.Where(d => d.TypeID == 2 && d.DKey == data.Origin).Select(d => d.DValue).FirstOrDefault();
}
// 数据接收模型
public class Plants
{
public int PlantID { get; set; }
public string Family { get; set; }
public string OriginName { get; set; }
}
原文地址:https://www.cnblogs.com/hao-1234-1234/p/11697558.html