我需要查询几个数据集。使用linq,我最终得到这样的代码..
var query =
from table1 in table_1.AsEnumerable()
join table2 in table_2.AsEnumerable()
on table1.Field<string>("Value1") equals
table2.Field<string>("value2") into join1
from joinData in join1.AsEnumerable()
join table1_1 in table_1.AsEnumerable()
on joinData.Field<string>("Value3") equals
table1_1.Field<string>("Value1")
into join2
from joinData2 in join2.AsEnumerable()
where joinData2.Field<string>("Value4") == "1" ||
joinData2.Field<string>("Value4") == "2" ||
joinData2.Field<string>("Value4") == "3" ||
joinData2.Field<string>("Value4") == "4" ||
joinData2.Field<string>("Value4") == "5" ||
joinData2.Field<string>("Value4") == "6" ||
joinData2.Field<string>("Value4") == "7" ||
joinData2.Field<string>("Value4") == "8"
select new
{
Value1 = data1.Field<string>("Value1"),
Value2 = data1.Field<string>("Value2"),
Value3 = data1.Field<string>("Value3"),
Value4 = data1.Field<string>("Value4"),
Value5 = data1.Field<string>("Value5"),
Value6 = data1.Field<string>("Value6"),
Value7 = data1.Field<string>("Value7"),
Value8 = data1.Field<string>("Value8"),
Value9 = data1.Field<string>("Value9"),
Value10 = data1.Field<string>("Value10"),
Value11 = data1.Field<string>("Value11"),
Value12 = data1.Field<string>("Value12"),
etc...
};
必须有一种更简单的方法来做到这一点。有没有办法只使用SQL语句查询数据集?
答案 0 :(得分:2)
您可以简化WHERE部分:
where joinData2.Field<string>("Value4") == "1" ||
joinData2.Field<string>("Value4") == "2" ||
joinData2.Field<string>("Value4") == "3" ||
where List<string>{ "1", "2", "3", ... }
.Contains( joinData2.Field<string>("Value4"))
也许你可以将SELECT部分重构为构造函数(非匿名类型)。
但不,你不能将SQL应用于数据集(超出你已经在使用LINQ做的事情)。
考虑使用像Entity框架这样的ORM,这会让生活变得更轻松。