我有两个var变量数据类型,我想两者都具有并集。我该怎么办?
var results = from table1 in dtTimeListTable.AsEnumerable()
join table2 in readyDataTable.AsEnumerable() on (decimal)table1["Avnr"] equals (int)table2["Avnr"]
orderby table2["Substation"], table2["S6_NAME"]
select new
{
Substation = (string)table2["Substation"],
S6_NAME = (string)table2["S6_NAME"],
ptime = ((DateTime)table1["DBTM"]).ToString("dd/MM/yyyy HH:mm").Substring(11, 5),
Wert = (decimal)table1["Wert"]
};
var resultsMaxMin = from table1 in dtMaxMin.AsEnumerable()
join table2 in readyDataTable.AsEnumerable() on (decimal)table1["Avnr"] equals (int)table2["Avnr"]
orderby table2["Substation"], table2["S6_NAME"]
select new
{
Substation = (string)table2["Substation"],
S6_NAME = (string)table2["S6_NAME"],
ptime =table1["ptime"],
Wert = (decimal)table1["Wert"]
};
答案 0 :(得分:0)
此代码段可能会有所帮助:
var list = new List<int>();
var sublist1 = list.Select(x => new { foo = 1, bar = 2, baz = "baz" });
var sublist2 = list.Select(x => new { bar = 3, foo = 1, baz = "baz" });
var sublist3 = list.Select(x => new { foo = 3, bar = "a", baz = "baz" });
var union1 = sublist1.Union(sublist2); // compilation error
var union2 = sublist1.Union(sublist3); // compilation error
仅当匿名类型与字段的名称,排序和 type 完全匹配时,Union()才起作用。如果在第二个选择中交换foo
和bar
的顺序,然后在第三个选择中将bar
转换为int值,则代码段中的错误就会消失。
在您的示例中,ptime
在两个列表之间的类型不同-字符串和对象。