c# - 避免for循环为网格显示带来表格相关信息

标签 c# .net asp.net-mvc entity-framework linq

我有以下数据库模式,我正在为其开发屏幕。

enter image description here

我想在网格中显示信息,例如 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
      });

此代码将生成带有JOINSELECT 并返回您的CustomTable 对象的List

它将仅从 TableB 中获取已填充 tableC_id 字段(NOT NULL)的行。

关于c# - 避免for循环为网格显示带来表格相关信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34765627/

相关文章:

mysql - MVC5中如何提高写入数据库的性能?

c# - 用于创建一系列单独对象的 Linq 方法?

c# - 是否可以从 .net 代码调试到 SQL Server 存储过程代码?

c# - 具有异步/等待的 ContextBoundObject

c# - 两个同名字段

asp.net-mvc - ASP.NET Mvc 2如何从javascript内部启动客户端验证?

c# - 扫描 64 位内存?

c# - Google Api 获取 token 请求返回 invalid_request

c# - 当在 'await' 方法上到达 'async' 时,线程会发生什么?

c# - 如何将一个 HttpClient 注入(inject)多个服务