使用Entity Framework进行3个表的联接查询,并返回特定列的数据,可以按照如下步骤:
1.定义需要查询的列
var columnsToSelect = new List<string>
{
"Table1.Column1", //第一个表中的列
"Table2.Column2", //第二个表中的列
"Table3.Column3" //第三个表中的列
};
2.使用LINQ的Join方法进行3个表的联接,并选择需要的列进行查询。
var result = dbContext.Table1
.Join(dbContext.Table2, t1 => t1.Table2Id, t2 => t2.Id, (t1, t2) => new { t1, t2 })
.Join(dbContext.Table3, t => t.t1.Table3Id, t3 => t3.Id, (t, t3) => new { t.t1, t.t2, t3 })
.Select(c => new {
Column1 = c.t1.Column1,
Column2 = c.t2.Column2,
Column3 = c.t3.Column3
}).ToList();
3.将结果转化为指定列名的列表返回。
List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
foreach (var item in result)
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add(columnsToSelect[0], item.Column1);
dictionary.Add(columnsToSelect[1], item.Column2);
dictionary.Add(columnsToSelect[2], item.Column3);
data.Add(dictionary);
}
最终得到的data即为所需要的结果数据。