【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<DAL.HRM_PersonalInformations>'无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.List<DAL.HRM_PersonalInformations>”
【发布时间】:2015-04-08 20:23:49
【问题描述】:

我正在尝试从数据库中获取一些数据并将它们绑定到下拉列表中。但出现以下错误:-

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;
            }
        }

这样对吗??谁能帮我 ?感谢提前..

【问题讨论】:

    标签: asp.net c#-4.0 webforms entity-framework-4


    【解决方案1】:

    嗯,在你的投影中,你有:

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

    并保持身体原样。您不能显式编写指定匿名类型的方法声明,因为它没有名称 - 但是List&lt;T&gt; 实现了IList,所以上面肯定会编译......如果你稍后尝试它会失败将其中的一个元素转换为 HRM_PersonalInformations

    编辑:如果字符串连接有问题,您可能需要在客户端进行。例如:

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

    【讨论】:

    • 我已经尝试过您的第一个提供方法...那时它无法识别“FullName”属性...。当我尝试您的最后一个提供方法并在程序执行后它给我以下错误。 . "{"无法创建'System.Object'类型的常量值。此上下文仅支持原始类型或枚举类型。"}"
    • @mgsdew: 那么FullNameHRM_PersonalInformations 的属性吗?我们不知道那种类型是什么样的。我不清楚最后一种情况的问题是什么 - 可能是由于字符串连接,但很难说。
    • 非常感谢...它在我的项目中运行良好:D
    【解决方案2】:

    这个方法等着你 HRM_PersonalInformations ,你为什么返回匿名类型?

    回到预期的类型很有用

    试试这个代码

     .Select(c => new HRM_PersonalInformations()
     {
         FullName  = c.FirstName 
         // set other prop 
     });
    

    【讨论】:

    • 我已经尝试过这种方式......那个时候它不识别'FullName'字段......因为在我的表格中没有'FullName'字段......只是它包含'FirstName' & '姓氏'..
    • 数据模型只能用于保存、更新过程。您可以创建业务模型 HRM_PersonalInformationsModel 并添加 FullName 道具。你回到 GetAssignePerson 方法上的商业模式
    • 是的。 HRM_PersonalInformation 包含“FirstName”和“LastName”属性的表。现在我想要 FullName 用于在下拉列表中显示数据...如何创建业务模型?我的意思是创建商业模式的过程..
    【解决方案3】:

    这是一个简单的示例,

    如果您想自定义数据业务流程或UI流程您需要一个新模型例如您希望在Dropdown中显示全名,

    • 添加新文件夹 MyCustomizeModels
    • 在 MyCustomizeModels 中添加新类 DropdownLookUpDecimal

    这个 DropdownLookUpDecimal 类。

      public class DropdownLookUpDecimal
        {
            public decimal Value { get; set; }
            public string Text { get; set; }
    
        }     
    

    你可以在项目中使用这个类所有的Dropdown。

      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 并用类填充列表并返回列表。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 2011-05-14
      • 2013-05-15
      相关资源
      最近更新 更多