嘿我试图填充我的模型,它的List<T>
和里面有List<T>
,但我不能让linq不允许我在Linq中创建一个.ToList。
这些是我的模特
CTIPOCHECKLIST:
public class CTIPOCHECKLIST
{
public int ID { get; set; }
public int ID_DEPTO{ get; set; }
public string DESCRIPCION { get; set; }
}
CTITLE:
public class CTITLE
{
public int ID { get; set; }
public int ID_TIPOCHECKLIST { get; set; }
public string Descripcion { get; set; }
public virtual CTIPOCHECKLIST Tipo_CheckList { get; set; }
public List<CSUBTITLE> Subtitulos { get; set; }
TROP.Models.TROPEntities db = new Models.TROPEntities();
public List<CTITLE> getAll(string codigo, int tipoCheckList)
{
List<CTITLE> model = new List<CTITLE>();
try
{
var result = (from a in db.TITLE
from t in db.Tipo_CheckList
where a.ID_TipoChecklist == t.ID
where a.ID_TipoChecklist == tipoCheckList
select new CTITLE
{
Descripcion = a.Descripcion,
ID = a.ID,
ID_TIPOCHECKLIST = a.ID_TipoChecklist,
Tipo_CheckList = new CTIPOCHECKLIST
{
DESCRIPCION = t.Descripcion,
ID = t.ID,
ID_DEPTO = t.ID_Depto
},
Subtitulos = (from s in db.SUBTITLE
where s.ID_TITLE == a.ID
select new CSUBTITLE
{
AMOUNTCK = s.AMOUNT_CK,
DESCRIPCION = s.DESCRIPCION,
ID = s.ID,
ID_IDTITLE = s.ID_TITLE,
NUMERACION = s.NUMERACION,
CHECKLIST = (from ck in db.CHECKLIST
where ck.ID_SUBTITLE == s.ID
&& ck.CODIGO == codigo
select new CCHECKLIST
{
CK = ck.CK,
CODIGO = ck.CODIGO,
ID = ck.ID,
ID_IDSUBTITLE = ck.ID_SUBTITLE,
USERCRE = ck.USERCRE,
USERMOD = ck.USERMOD
}).FirstOrDefault()
})//here I put ToList
}).ToList();
return result;
}
catch
{
}
return model;
}
CSUBTITLE:
public class CSUBTITLE
{
public int ID { get; set; }
public int ID_IDTITLE { get; set; }
public string NUMERACION { get; set; }
public string DESCRIPCION { get; set; }
public bool AMOUNTCK { get; set; }
public CTITLE TITLE { get; set; }
public CCHECKLIST CHECKLIST { get; set; }
}
CCHECKLIST:
public class CCHECKLIST
{
public int ID { get; set; }
public int ID_IDSUBTITLE { get; set; }
public string CODIGO { get; set; }
public bool CK { get; set; }
public string USERCRE { get; set; }
public string USERMOD { get; set; }
public CSUBTITLE SUBTITLE { get; set; }
}
我的主类是CTITLE,其GetAll方法类型为List 我只是想填充这个查询,但CTITLE的Subtitulos属性,它的列表当我使用类型IEnumerable填充它工作,但我不希望它返回IEnumerable我需要列表但当我把属性Subtitle转为List和在查询LinQ中添加.ToList()不允许我,我花了3天时间试图解决这个问题。我想不出来。
答案 0 :(得分:0)
这里的问题是,您希望在Linq-to-entities操作中执行Linq-to-objects查询操作。
Linq-to-entities表达式树被转换为SQL并从数据库中获取数据。之后,对象被实现为.Net对象。只有在此步骤之后,才能调用.ToList()
因此,这意味着,您可以稍微重新构建查询并延迟.ToList()
次调用:
var result =
(from a in db.TITLE
from t in db.Tipo_CheckList
where a.ID_TipoChecklist == t.ID
where a.ID_TipoChecklist == tipoCheckList
let subtitulos = from s in db.SUBTITLE
where s.ID_TITLE == a.ID
select new CSUBTITLE {
AMOUNTCK = s.AMOUNT_CK,
DESCRIPCION = s.DESCRIPCION,
ID = s.ID,
ID_IDTITLE = s.ID_TITLE,
NUMERACION = s.NUMERACION,
CHECKLIST = (from ck in db.CHECKLIST
where ck.ID_SUBTITLE == s.ID
&& ck.CODIGO == codigo
select new CCHECKLIST
{
CK = ck.CK,
CODIGO = ck.CODIGO,
ID = ck.ID,
ID_IDSUBTITLE = ck.ID_SUBTITLE,
USERCRE = ck.USERCRE,
USERMOD = ck.USERMOD
}).FirstOrDefault()
}
let ctitle = new CTITLE {
Descripcion = a.Descripcion,
ID = a.ID,
ID_TIPOCHECKLIST = a.ID_TipoChecklist,
Tipo_CheckList = new CTIPOCHECKLIST
{
DESCRIPCION = t.Descripcion,
ID = t.ID,
ID_DEPTO = t.ID_Depto
}
}
select new { CTITLE = ctitle, SUBTITULOS = subtitulos }
)
.Select(t => t.CTITLE.Subtitulos = t.SUBTITULOS.ToList())
.Select(t => t.CTITLE)
.ToList()
这可以像上面的陈述一样(模拟一些拼写错误和括号)。