LINQ中的匿名类型

时间:2011-05-06 14:13:01

标签: linq anonymous-types

我使用以下代码从数据库表tblDatabases获取记录。然后,我根据值填充表单上的控件。我已经使用了一些函数来获取在文本框中显示所需的值(例如,显示值与值不同。

DetailData是我的基本表单类中的对象。最初我只是从表中得到了记录,我能够将DetailData转换为tblDatabases并使用反射来获取数据的所有值并填充表单上的控件。

由于匿名类型,我无法再将DetailData转换为我的表格。

我希望能够在DetailData上使用反射来获取值。

谢谢, 布拉德

  DetailData = (from db in priorityDataContext.tblDatabases
      where db.DatabaseID == Id
      select new
        {
            db.DatabaseID,
            db.DatabaseName,
            db.Purpose,
            db.BackEnd,
            db.FrontEnd,
            db.Version,
            db.ProducesReports,
            db.MultiUser,
            db.UserDescription,
            Developer = priorityDataContext.func_get_employee_name(db.Developer),
            DeptOwner = priorityDataContext.func_get_dept_name(db.DeptOwner),
            db.Source_Code_Path,
            db.Notes,
            db.Active,
            db.row_entry_time_stamp,
            row_oper_name = priorityDataContext.func_get_employee_name(db.Developer),
            db.row_last_chng_time_stamp,
            row_last_chng_oper_name = priorityDataContext.func_get_employee_name(db.Developer)
        }).SingleOrDefault();


protected virtual void PopulateDetailControlsA(List<Control> controlContainers, string srcDataTableName)
{
    switch (srcDataTableName)
    {
        case "tblDatabase" :
            break;
    }

    var database = (tblDatabase) DetailData;
    var type = typeof(tblDatabase);
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (var controlContainer in controlContainers)
    {
        foreach (var propertyInfo in properties)
        {
            if (!ControlExists(controlContainer, propertyInfo.Name)) continue;

            var txtExtControl = controlContainer.Controls[propertyInfo.Name] as ExtendedTextBox;
            if (txtExtControl != null)
            {
                try
                {
                    var value = propertyInfo.GetValue(database, null).ToString();

                    txtExtControl.Text = value;
                }
                catch (NullReferenceException)
                {
                }
                continue;
            }

            var lnklblControl = controlContainer.Controls[propertyInfo.Name] as ExtendedLinkLabel;
            if (lnklblControl != null)
            {
                try
                {
                    var value = propertyInfo.GetValue(database, null).ToString();

                    lnklblControl.Text = value;
                }
                catch (NullReferenceException)
                {
                }
                continue;
            }

            var chkControl = controlContainer.Controls[propertyInfo.Name] as ExtendedCheckBox;
            if (chkControl != null)
            {
                try
                {
                    var value = propertyInfo.GetValue(database, null).ToString();

                    switch (value)
                    {
                        case "True":
                            chkControl.CheckState = CheckState.Checked;
                            break;
                        case "False":
                            chkControl.CheckState = CheckState.Unchecked;
                            break;
                    }
                }
                catch (NullReferenceException)
                {
                    chkControl.CheckState = CheckState.Indeterminate;
                }
                continue;
            }

            var cmbControl = controlContainer.Controls[propertyInfo.Name] as ExtendedComboBox;
            if (cmbControl != null)
            {
                try
                {
                    var value = propertyInfo.GetValue(database, null).ToString();

                    cmbControl.ValueMember = value;
                }
                catch (Exception ex)
                {
                }
                continue;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您使用的UI技术是什么?如果你可以使用绑定,你不需要担心匿名类型的反射,如果你需要格式化/计算这个值,也可以使用转换器。

从您的回复中,您不能只使用连接到linq,然后将其绑定到组合框?

    private void Form1_Load(object sender, System.EventArgs e)
    {
        var item = new DataClassesDataContext();
        var stuff = item.Entity.Where(c => c.Property.Contains("something"));
        comboBox1.DataSource = stuff;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "PIN";
    }