我有以下数据库模式,我正在为其开发屏幕。
我想在网格中显示信息,例如 Desc 应该在多行中重复,并且每一行都必须有来自 tableC,tableB and tableA相关信息。
现在我正在使用 linq“Include”相关实体获取 Controller 层中的每个表,并使用许多 foreach 循环我正在创建 customTable 类,然后将其绑定(bind)到剑道网格。
foreach(var a in table C)
{
foreach(var b in tableB)
{
CustomTable c = new CustomTable {
tableDesc = b.Desc,
tableBDesc = a.Desc
}
}
}
class CustomTable
{
public string tableDDesc{get;set;}
public string tableBDesc {get;set;}
}
我在想是否有更好的方法可以让这个/Linq 语法在数据访问层本身中构建 customClass。请输入任何信息?
最佳答案
如果您使用 EF,您可以这样做:
_context.TableC.Select(x => x.TableB)
.Select(x => new CustomTable
{
tableDesc = x.TableC.Desc,
tableBDesc = x.Desc
});
此代码将生成带有JOIN
的SELECT
并返回您的CustomTable
对象的List
。
它将仅从 TableB
中获取已填充 tableC_id
字段(NOT NULL)的行。
关于c# - 避免for循环为网格显示带来表格相关信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34765627/