![](/img/trans.png)
[英]How to use Entity Framework 6 LINQ query for Dynamic tables with same structure?
[英]How to structure a dynamic query with linq
我正在嘗試建立一個可以扔到電子表格中的數據集,我必須承認我已經達到飽和點! 這些是我的表:
**Matrix**
Employee id
Template id
Expiry date
**Employee**
Id
Employee Name
**Template**
Template Id
Template Name
我想要實現的是所有員工的列表(員工將組成數據集的行)和所有模板的列表(模板是列標題),然后我需要為每個模板的每個員工填充網格以及到期日。 一些員工尚未獲得每個模板的有效期,因為他們尚未獲得認證。
我當時正在考慮創建一個雇員列表,然后添加該雇員的Matrix對象集合,但這並不一定奏效,因為並非每個人都將每種模板類型都作為矩陣中的條目。
我已經與linq一起工作了幾年,但是由於構建此數據集的動態性,我在這里有些困惑。 定期添加新模板,因此無需維護即可。 甚至可以通過使用linq來實現,還是我需要看看在SQL中構建視圖?
任何幫助將不勝感激。 以下是我正在嘗試做的一個簡單示例!
Temp1 temp2 temp3 temp4
Emp1 01/01/2014 02/6/2015 04/06/2012
Emp2 02/6/2015
Emp3 01/05/2010
這是我的數據結構:
編輯
好吧,我確實設法使某些東西可以以我描述的格式輸出到視圖中,但是我不知道它是否特別有效-如果有更好的方法,我想知道如何!
public class EmpMatrix
{
public int TemplateId { get; set; }
public string TemplateName { get; set; }
public DateTime? ExpiryDate { get; set; }
}
public class Classemptemp
{
public emp Employee { get; set; }
public List<EmpMatrix> tempateList { get; set; }
}
public DateTime? GetExpiry(int template, int empl)
{
return (from a in _entities.matrices
where a.empid == empl && a.tempId == template
select a.expiryDate).FirstOrDefault();
}
public List<Classemptemp> testing()
{
List<emp> employees = _entities.emps.ToList();
List<template> TemplateList = _entities.templates.ToList();
List<Classemptemp> empList = (from employee in employees
let matrix = TemplateList.Select(template => new EmpMatrix
{
TemplateId = template.templateId, TemplateName = template.Name, ExpiryDate = GetExpiry(template.templateId, employee.Id)
}).ToList()
select new Classemptemp
{
Employee = employee, tempateList = matrix
}).ToList();
return empList;
}
類(如果datacontext仍然存在,則將IEnumerable更改為IQueryable):
public MyViewModel {
IEnumerable<emp> emps;
IEnumerable<template> templates;
}
控制器:
var emps=db.emps
.Include(e=>e.matrices)
.Include(e=>e.matrices.template);
var templates=db.templates;
var model=new MyViewModel { emps=emps,templates=templates };
return model;
視圖:
@model MyViewModel
<thead><tr>
@foreach(var template in model.templates)
{
<th>@template.Name</th>
}
</tr></thead>
<tbody>
@foreach(var emp in model.emps)
{
<tr>
@foreach(var template in model.templates)
{
<td>
@(emp.matrices.Any(m=>m.templateId==template.templateId)?
emp.matrices.First(m=>m.templateId==template.templateId).toString("G"):
"")
</td>
}
</tr>
}
</tbody>
我以為您可以使用System.Data.DataTable類並執行以下操作:
public class MatrixCell
{
public string EmployeeName { get; set; }
public string ColumnName { get; set; }
public DateTime Date { get; set; }
public DataTable ProcessRow(DataTable table)
{
bool found = false;
foreach(DataRow row in table.Rows)
{
if ((string)row["Employeename"] == EmployeeName)
{
found = true;
row[ColumnName] = Date;
break;
}
}
if(!found)
{
DataRow row = table.NewRow();
row["Employeename"] = EmployeeName;
row[ColumnName] = Date;
table.Rows.Add(row);
}
return table;
}
}
每個MatrixTable對象代表表中的一個單元格。 申請證明:
class Program
{
static void Main(string[] args)
{
using (var db = new TestDB())
{
//Initialize DataTable
DataTable table = new DataTable();
var columnNames = db.Templates.Select(t => t.TemplateName).ToArray();
table.Columns.Add("Employeename", typeof(string));
foreach (var name in columnNames)
{
DataColumn column = new DataColumn(name, typeof(DateTime));
column.AllowDBNull = true;
table.Columns.Add(column);
}
//Get Matrix objects
var result = db.Matrices.Select(m => new MatrixCell
{
ColumnName = m.Template.TemplateName,
Date = (DateTime)m.Date,
EmployeeName = m.Employee.EmployeeName
}).ToArray();
//Populate datatable
foreach (var matrix in result)
table = matrix.ProcessRow(table);
Console.Read();
}
}
}
這將創建一個以表名作為列名的數據表。 每行代表一名雇員,並填寫了相應模板的Datetimes。然后,您可以將此DataTable傳遞到視圖以顯示它。
我希望這可以工作!
編輯:我測試了幾個db值,它似乎正在工作。 可以將Employeename
作為主鍵而不是列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.