如何使用linq

时间:2015-11-20 11:29:30

标签: c# sql asp.net-mvc entity-framework 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      

这是我的数据结构:

enter image description here

修改

好吧,我确实得到了一些工作,我可以输出到我所描述的格式的视图,但我不知道它是否特别有效 - 如果有更好的方式我会喜欢知道怎么做!

   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;
    }

2 个答案:

答案 0 :(得分:0)

Class(如果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>

答案 1 :(得分:0)

我原以为你可以使用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();
        }

    }
}

这将创建一个DataTable,其中templatenames作为列名。每行代表一名员工,并填写相应模板的日期时间。然后您可以将此DataTable传递给您的视图以显示它。

我希望这可行!

编辑:我测试了几个db值,它似乎正在工作。可以使Employeename成为主键而不是列。