如何在mvc5中使用linq to sql连接来显示数据 [英] how to use join in linq to sql in mvc5 to show data

查看:138
本文介绍了如何在mvc5中使用linq to sql连接来显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:附加信息:无法在LINQ to Entities查询中构造实体或复杂类型GHMSaModel.TblGAdrsCity。







Error: Additional information: The entity or complex type 'GHMSaModel.TblGAdrsCity' cannot be constructed in a LINQ to Entities query.



public IEnumerable<TblGAdrsCity> Index()
       {

           IList<TblGAdrsCity> CityList = new List<TblGAdrsCity>();

           var data = (from c in context.TblGAdrsCities
                      join st in context.TblGAdrsStates on c.TblGStateID equals st.StateID
                      join ct in context.TblGAdrsCountries on c.tblGCountryID equals ct.CountryID
                      select c //new TblGAdrsCity{ CityID = c.CityID, CityName = c.CityName,VerifiedStatus=c.VerifiedStatus,CreatedBy=c.CreatedBy,CreatedOn=c.CreatedOn,ActiveStatus=c.ActiveStatus,ModifiedBy=c.ModifiedBy,ModifiedOn=c.ModifiedOn,Remarks=c.Remarks,TblGStateID=st.StateID,tblGCountryID=ct.CountryID}
                      );
           //CityList = data.ToList();
           CityList = data.ToList();
           return (CityList);
       }

推荐答案

狂野猜测:将您的查询转换为ToList()然后返回。

Wild guess: Convert your query to ToList() and then return.
public IEnumerable<tblgadrscity> Index()
       {
 
           IList<tblgadrscity> CityList = new List<tblgadrscity>();
 
           var data = (from c in context.TblGAdrsCities
                      join st in context.TblGAdrsStates on c.TblGStateID equals st.StateID
                      join ct in context.TblGAdrsCountries on c.tblGCountryID equals ct.CountryID
                      select new TblGAdrsCity{ CityID = c.CityID, CityName = c.CityName,VerifiedStatus=c.VerifiedStatus,CreatedBy=c.CreatedBy,CreatedOn=c.CreatedOn,ActiveStatus=c.ActiveStatus,ModifiedBy=c.ModifiedBy,ModifiedOn=c.ModifiedOn,Remarks=c.Remarks,TblGStateID=st.StateID,tblGCountryID=ct.CountryID}).ToList();
           return (data);
       }</tblgadrscity></tblgadrscity></tblgadrscity>





-KR



-KR


我确实建议阅读这些主题:

该实体不能在LINQ中构建到实体查询 [ ^ ]

e在LINQ to Entities查询中无法构造ntity或复杂类型'[ ^ ]

错误:无法构造实体或复杂类型LINQ to Entities查询 [ ^ ]



答案是:Linq-to-Entities只能投射到匿名类型或普通类( DTO [ ^ ])。您无法投影到映射的实体。
I really do suggest to read these threads:
The entity cannot be constructed in a LINQ to Entities query[^]
The entity or complex type ' ' cannot be constructed in a LINQ to Entities query[^]
Error: The entity or complex type cannot be constructed in a LINQ to Entities query[^]

The answer is: Linq-to-Entities can only project to an anonymous type or a regular class(DTO[^]). You can't project onto a mapped entity.


错误:数据中的Count = 0(数据中没有值)











var data =(来自c in context.TblGAdrsCities

在上下文中连接st.TblGAdrsStates在c.TblGStateID上等于st.StateID

加入ct在context.TblGAdrsCountries上c.tblGCountryID等于ct.CountryID

选择新的Customizeview

{

CityID = c.CityID,

CityName = c.CityName,

VerifiedStatus = c.VerifiedStatus,

CreatedBy = c.CreatedBy,

CreatedOn = c.CreatedOn,

ActiveStatus = c.ActiveStatus,

ModifiedBy = c.ModifiedBy,
ModifiedOn = c.ModifiedOn,

备注= c。备注,

StateName = st.StateName,



})。ToList();











返回查看(数据);

}
Error:Count=0 in data(no value in data )





var data = (from c in context.TblGAdrsCities
join st in context.TblGAdrsStates on c.TblGStateID equals st.StateID
join ct in context.TblGAdrsCountries on c.tblGCountryID equals ct.CountryID
select new Customizeview
{
CityID = c.CityID,
CityName = c.CityName,
VerifiedStatus = c.VerifiedStatus,
CreatedBy = c.CreatedBy,
CreatedOn = c.CreatedOn,
ActiveStatus = c.ActiveStatus,
ModifiedBy = c.ModifiedBy,
ModifiedOn = c.ModifiedOn,
Remarks = c.Remarks,
StateName = st.StateName,

}).ToList();





return View(data);
}


这篇关于如何在mvc5中使用linq to sql连接来显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆