从linq query数据,创建一个实体 或者说 class
我的工作是做cad开发的 基本上是在别人已经做好的架构上做第二次开发 有时候需要 linq query 结果,作为一个实体来使 或者说是一个类
为了方便 我建立子程序 用linq query的结果建立一个新的实体 贴在这里供大家,讨论 完善
linq query 结果,是不能够作为变量传输的 它是一个 anonymou
这段程序 将 anonymous 作为 dynamic分析 建立属性 从而构成一个类
类中有一个特殊的Dictionaryn rr 用来兼容大小写 (主要是我的数据来自于oracle和 Sql server)
仅仅是一个例子,
var s_out = pp.
Cfc_list.Select(p => new
{
type = pp.T_br,
len = string.Format("{0:F2}", pp.Len),
Part = pp.P_part
aaa=p.aaa
};
//var test = tool_create_class(s_out, "print_1");
新的类 被创建 在 test 中 类的名字 print_1
所以我们有
List <print_1> outt= s_out.Select(q=> new print_1(q)).Tolist();
private string tool_create_class(IEnumerable<dynamic> onym_s, string class_name)
{
dynamic onym = onym_s.First();
var anonymous_types = onym.GetType().GetProperties();
string r_r = "public class " + class_name + "\n{" +
"\n\tpublic " + class_name + "( DataRow r)\n\t{\t\n";
string r_r_e =
"\n\tpublic " + class_name + "( dynamic onym)\n\t {\t\n";
r_r += "\t var rr = new Dictionary<String, String>();\n";
r_r += "\t foreach (DataColumn a_pro in r.Table.Columns)\n\t {\n";
r_r += "\t rr.Add(((String)a_pro.ColumnName).ToUpper(), (String)a_pro.ColumnName);\n";
r_r += " \t }\n";
r_r_e += "\t var rr = new Dictionary<String, String>();\n";
r_r_e += "\t foreach (PropertyInfo a_pro in onym.GetType().GetProperties())\n\t {\n";
r_r_e += "\t rr.Add(((String)a_pro.Name).ToUpper(), (String)a_pro.Name);\n";
r_r_e += " \t }\n";
string new_eq = "";
string new_eq_e = "";
string prop = "";
int cur_col_num = 0;
foreach (var a_type in anonymous_types)
{
string col_name = a_type.Name;
string col_type = a_type.PropertyType.ToString();
var name_name = a_type.PropertyType.Name;
var full_name = a_type.PropertyType.FullName;
new_eq += "\t" + col_name + " = ";
new_eq_e += "\t" + col_name + " = ";
if (col_type.Contains("System.String"))
{
prop += "\tpublic " + "string" + " " + col_name + " { get; set; } \n";
new_eq += "\t r[rr[\"" + col_name.ToUpper() + "\"]]== DBNull.Value ? \"\" : Convert.ToString(r[rr[\"" + col_name.ToUpper() + "\"]]); \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? \"\" : \n\t" +
" onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null); \n";
}
else if (col_type.Contains("System.Guid"))
{
prop += "\tpublic " + "Guid" + " " + col_name + " { get; set; } \n";
new_eq += " r[rr[\"" + col_name.ToUpper() + "\"]]== DBNull.Value ? \n" +
"\t Guid.NewGuid() : Guid.Parse(r[rr[\"" + col_name.ToUpper() + "\"]].ToString()) ; \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? Guid.NewGuid() : \n\t" +
" Guid.Parse(onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null).ToString() ) ; \n";
}
else if (col_type.Contains("System.Int32"))
{
prop += "\tpublic " + "Int32" + " " + col_name + " { get; set; } \n";
new_eq += " r[rr[\"" + col_name.ToUpper() + "\"]]== DBNull.Value ? 0 : Convert.ToInt32(r[rr[\"" + col_name.ToUpper() + "\"]]); \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? 0 : \n\t" +
" Convert.ToInt32( onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ) ; \n";
}
else if (col_type.Contains("System.Double") || col_type.Contains("System.Decimal") || col_type.Contains("System.Single"))
{
prop += "\tpublic " + "Double" + " " + col_name + " { get; set; } \n";
new_eq += " r[rr[\"" + col_name.ToUpper() + "\"]]== DBNull.Value ? 0.0 :Convert.ToDouble(r[rr[\"" + col_name.ToUpper() + "\"]]); \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? 0.0 : \n\t" +
" Convert.ToDouble( onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ) ; \n";
}
else if (col_type.Contains("Boolean"))
{
prop += "\tpublic " + "Boolean" + " " + col_name + " { get; set; } \n";
new_eq += " r[rr[\"" + col_name.ToUpper() + "\"]]== DBNull.Value ? false : Convert.ToBoolean(r[rr[\"" + col_name.ToUpper() + "\"]]); \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? false : \n\t" +
" Convert.ToBoolean( onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ) ; \n";
}
else if (!col_type.Contains("System"))
{
prop += "\tpublic " + name_name + "_ " + col_name + " { get; set; } \n";
new_eq += "typeof(" + name_name + "_).IsInstanceOfType( r[rr[\"" + col_name.ToUpper() + "\"]]) ? " +
" r[rr[\"" + col_name.ToUpper() + "\"]]:DBNull.Value; \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? false : \n\t" +
" onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ) ; \n";
}
else if (col_type.Contains("System.DateTime"))
{
prop += "\tpublic " + "System.DateTime" + " " + col_name + " { get; set; } \n";
new_eq += " r[rr[\"" + col_name.ToUpper() + "\"]]== DBNull.Value ? DateTime.Now : Convert.ToDateTime(r[rr[\"" + col_name.ToUpper() + "\"]]); \n";
new_eq_e += "onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ==null ? DateTime.Now : \n\t" +
" Convert.ToDateTime( onym.GetType().GetProperty(rr[\"" + col_name.ToUpper() + "\"]).GetValue(onym, null) ) ; \n";
}
else
{
new_eq += "error error " + col_type;
}
cur_col_num++;
}
new_eq = new_eq.TrimEnd('\n');
prop = prop.TrimEnd('\n');
r_r += new_eq + "\n\t}\n\n";
r_r += r_r_e;
r_r += new_eq_e + "\n\t}\n\n";
r_r += prop + "\n}";
return r_r;
}