哪种类型会返回字段方法?

时间:2016-05-23 05:15:41

标签: c# wpf

我无法弄清楚如何获取LINQ结果的类型。我的集合包含字符串和bool,所以当我尝试对行进行操作时遇到麻烦。我使用try / catch(笑声)附加了一个令人难以置信的粗暴锻炼,但它伤害了我的灵魂,并且更愿意知道获得Type的正确方法。

    private AppointmentInfoClass UpdateDataContext(DataSet phaseDataSet) {

        var phaseCollection = new AppointmentInfoClass();
        var Type = phaseCollection.GetType();
        var properties = Type.GetProperties();

        var result = from DataRow myRow in DataBindings.CompanyAppsDataSet.Tables[0].Rows
                     where (int)myRow["AppointmentID"] == ApptID
                     select myRow;

        var k = 0; 
        foreach (DataRow row in phaseDataSet.Tables[0].Rows) {
            string header;

            header = row.Field<string>("Header");

            foreach (var field in result) {     
                try {
                    properties[k].SetValue(phaseCollection, field.Field<string>(header));
                }
                catch (Exception) {

                    properties[k].SetValue(phaseCollection, field.Field<bool>(header).ToString());
                }
            }
            k++;
        }
        return phaseCollection;
    }

1 个答案:

答案 0 :(得分:1)

它将返回您编写的类型而不是Type

string s = field.Field<string>("ColumnName");

bool b = field.Field<bool>("ColumnName");