Unable to create "anonymous type" type of constant value. This context only supports primitive or enumeration type.

 

// Get the data table category

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();

// get the data of the plant

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();

 

The right approach

if (data != null)
{
  data.OriginName = typeList.Where(d => d.TypeID == 2 && d.DKey == data.Origin).Select(d => d.DValue).FirstOrDefault();

}

 

// Data Model received
public class Plants
{

public int PlantID { get; set; }
public string Family { get; set; }

public string OriginName { get; set; }

}

 

Guess you like

Origin www.cnblogs.com/hao-1234-1234/p/11697558.html