无法将类型 'System.Collections.Generic.List<AnonymousType#1>' 隐式转换为 'System.Collections.Generic.List<DAL.HRM_PersonalInformations>'
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<DAL.HRM_PersonalInformations>'
我正在尝试从数据库中获取一些数据并将它们绑定到下拉列表中。但是出现以下错误:-
public virtual List<HRM_PersonalInformations>GetAssignePerson(String OCODE, int dptID){
var query = (context.HRM_PersonalInformations.Where
(c => c.OCODE == OCODE && c.DepartmentId == dptID)
.Select(c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID})).ToList();
return query; // It indicate error line
}
之后,我尝试在下拉列表中绑定数据,我的代码如下:-
private void FillAssignPerson()
{
try
{
string OCODE = ((SessionUser)Session["SessionUser"]).OCode;
int dptId = Convert.ToInt32(ddlAssignPerDept.SelectedValue);
var row = enquiryBll.GetAssignePerson(OCODE, dptId).ToList();
//const string list = "SELECT FirstName + ' ' + LastName AS FullName, EID, FirstName, LastName " +
// "FROM HRM_PersonalInformations " +
// "ORDER BY EID ASC"
if (row.Count > 0)
{
ddlAssignPerson.Items.Clear();
ddlAssignPerson.DataSource = row;
ddlAssignPerson.DataTextField = "FullName";
ddlAssignPerson.DataValueField = "EID";
ddlAssignPerson.DataBind();
ddlAssignPerson.Items.Insert(0, new ListItem("----- Select One -----", "0"));
ddlAssignPerson.AppendDataBoundItems = false;
}
}
这样做对吗??谁能帮我 ?感谢提前..
嗯,在你的投影中你有:
c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID}
这是创建一个匿名拼写错误的实例 - 而不是 HRM_PersonalInformations
的实例。如果这些是您在 HRM_PersonalInformations
中唯一需要的两个属性,您可以将其更改为:
c => new HRM_PersonalInformations {
FullName = (c.FirstName + ' ' + c.LastName),
EID = c.EID
}
或者根据您的查询判断,您可能根本不需要投影。您可能会觉得:
return context.HRM_PersonalInformations
.Where(c => c.OCODE == OCODE && c.DepartmentId == dptID)
.ToList();
或者,如果您只需要这些属性而不真的需要列表中的项目成为 HRM_PersonalInformations
的实例,您可以将方法的签名更改为:
public virtual IList GetAssignePerson(...)
并保持 body 不变。您不能显式编写指定匿名类型的方法声明,因为它没有名称 - 但 List<T>
实现 IList
,所以上面肯定会编译......如果不过,您稍后会尝试将其中的一个元素转换为 HRM_PersonalInformations
。
编辑:如果字符串连接有问题,您可能需要这样做client-side。例如:
public IList GetAssignePerson(String OCODE, int dptID) {
return context.HRM_PersonalInformations
.Where(c => c.OCODE == OCODE && c.DepartmentId == dptID)
.Select(c => new { c.FirstName, c.LastName, c.EID })
.AsEnumerable() // Do the rest client-side
.Select(c => new {
FullName = c.FirstName + " " + c.LastName,
c.EID
})
.ToList();
}
这个方法等着你HRM_PersonalInformations,为什么你是return匿名类型?
回到预期类型很有用
试试这个代码
.Select(c => new HRM_PersonalInformations()
{
FullName = c.FirstName
// set other prop
});
这是一个简单的示例,
如果您想自定义数据业务流程或 UI 流程,您需要一个新模型,例如您希望在下拉列表中显示全名,
- 添加新文件夹 MyCustomizeModels
- 在 MyCustomizeModels 中添加新的 class DropdownLookUpDecimal
this DropdownLookUpDecimal class.
public class DropdownLookUpDecimal
{
public decimal Value { get; set; }
public string Text { get; set; }
}
You can use this class all Dropdown in the project.
public List<DropdownLookUpDecimal>GetAssignePerson(string oCode, int dptID)
{
var query = context.HRM_PersonalInformations.Where
(c => c.OCODE == oCode&& c.DepartmentId == dptID)
.Select(c => new DropdownLookUpDecimal
{
Text = c.FirstName + ' ' + c.LastName,
Value = c.EID
});
return query.ToList();
}
只需创建新的 .cs 并用 class 和 return 列表填充列表。
希望对您有所帮助
我正在尝试从数据库中获取一些数据并将它们绑定到下拉列表中。但是出现以下错误:-
public virtual List<HRM_PersonalInformations>GetAssignePerson(String OCODE, int dptID){
var query = (context.HRM_PersonalInformations.Where
(c => c.OCODE == OCODE && c.DepartmentId == dptID)
.Select(c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID})).ToList();
return query; // It indicate error line
}
之后,我尝试在下拉列表中绑定数据,我的代码如下:-
private void FillAssignPerson()
{
try
{
string OCODE = ((SessionUser)Session["SessionUser"]).OCode;
int dptId = Convert.ToInt32(ddlAssignPerDept.SelectedValue);
var row = enquiryBll.GetAssignePerson(OCODE, dptId).ToList();
//const string list = "SELECT FirstName + ' ' + LastName AS FullName, EID, FirstName, LastName " +
// "FROM HRM_PersonalInformations " +
// "ORDER BY EID ASC"
if (row.Count > 0)
{
ddlAssignPerson.Items.Clear();
ddlAssignPerson.DataSource = row;
ddlAssignPerson.DataTextField = "FullName";
ddlAssignPerson.DataValueField = "EID";
ddlAssignPerson.DataBind();
ddlAssignPerson.Items.Insert(0, new ListItem("----- Select One -----", "0"));
ddlAssignPerson.AppendDataBoundItems = false;
}
}
这样做对吗??谁能帮我 ?感谢提前..
嗯,在你的投影中你有:
c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID}
这是创建一个匿名拼写错误的实例 - 而不是 HRM_PersonalInformations
的实例。如果这些是您在 HRM_PersonalInformations
中唯一需要的两个属性,您可以将其更改为:
c => new HRM_PersonalInformations {
FullName = (c.FirstName + ' ' + c.LastName),
EID = c.EID
}
或者根据您的查询判断,您可能根本不需要投影。您可能会觉得:
return context.HRM_PersonalInformations
.Where(c => c.OCODE == OCODE && c.DepartmentId == dptID)
.ToList();
或者,如果您只需要这些属性而不真的需要列表中的项目成为 HRM_PersonalInformations
的实例,您可以将方法的签名更改为:
public virtual IList GetAssignePerson(...)
并保持 body 不变。您不能显式编写指定匿名类型的方法声明,因为它没有名称 - 但 List<T>
实现 IList
,所以上面肯定会编译......如果不过,您稍后会尝试将其中的一个元素转换为 HRM_PersonalInformations
。
编辑:如果字符串连接有问题,您可能需要这样做client-side。例如:
public IList GetAssignePerson(String OCODE, int dptID) {
return context.HRM_PersonalInformations
.Where(c => c.OCODE == OCODE && c.DepartmentId == dptID)
.Select(c => new { c.FirstName, c.LastName, c.EID })
.AsEnumerable() // Do the rest client-side
.Select(c => new {
FullName = c.FirstName + " " + c.LastName,
c.EID
})
.ToList();
}
这个方法等着你HRM_PersonalInformations,为什么你是return匿名类型?
回到预期类型很有用
试试这个代码
.Select(c => new HRM_PersonalInformations()
{
FullName = c.FirstName
// set other prop
});
这是一个简单的示例,
如果您想自定义数据业务流程或 UI 流程,您需要一个新模型,例如您希望在下拉列表中显示全名,
- 添加新文件夹 MyCustomizeModels
- 在 MyCustomizeModels 中添加新的 class DropdownLookUpDecimal
this DropdownLookUpDecimal class.
public class DropdownLookUpDecimal
{
public decimal Value { get; set; }
public string Text { get; set; }
}
You can use this class all Dropdown in the project.
public List<DropdownLookUpDecimal>GetAssignePerson(string oCode, int dptID)
{
var query = context.HRM_PersonalInformations.Where
(c => c.OCODE == oCode&& c.DepartmentId == dptID)
.Select(c => new DropdownLookUpDecimal
{
Text = c.FirstName + ' ' + c.LastName,
Value = c.EID
});
return query.ToList();
}
只需创建新的 .cs 并用 class 和 return 列表填充列表。
希望对您有所帮助