无法创建“匿名类型”类型的常量值。此上下文仅支持基元类型或枚举类型。
作者:互联网
// 获取类别表数据
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; }
}
标签:TypeID,Where,基元,枚举,PlantID,类型,data,public,Select 来源: https://www.cnblogs.com/hao-1234-1234/p/11697558.html