显示实体框架中列数据的列名称

时间:2017-08-05 10:00:44

标签: c# entity-framework

我有一个具有以下结构的数据。我正在使用实体框架。

Id     fileName   date                 status
---------------------------------------------
 1     file1      12-05-2016 11:30     fail
 2     file1      12-05-2016 11:35     success
 3     file2      13-05-2016 12:01     success
 4     file2      13-05-2016 12.02     fail
 5     file1      13-05-2016           success
 6     file3      13-05-2016           fail

我想要这样的结果

fileName       12-05-2016   13-05-2016
------------------------------------------
file1          success        sucess
file2                         fail
file3                         fail

标准 - 在两个日期之间搜索数据,所有日期文件状态应以上述方式显示。同一天,同一文件中存在两个条目,然后将显示最新状态。

我正在使用代码优先方法来获取数据。

这是我在repo的代码

     IQueryable<FileData> filedata=this.context.FileData;

3 个答案:

答案 0 :(得分:1)

I created a pivot table based on Svek code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStatus filestatus = new FileStatus();
            filestatus.Load();
            DataTable pivot = filestatus.PivotTable();
        }
    }
    public class FileStatus
    {
        public static List<FileStatus> filedata = new List<FileStatus>();

        public int? Id { get; set; }
        public string filename { get; set; }
        public DateTime date { get; set; }
        public string status { get; set; }

        public void Load()
        {
            filedata = new List<FileStatus>()
            {
                new FileStatus(){ Id = 1, filename = "file1", date = new DateTime(2016,05,12,11,30,00), status = "fail"},
                new FileStatus(){ Id = 2, filename = "file1", date = new DateTime(2016,05,12,11,35,00), status = "success"},
                new FileStatus(){ Id = 3, filename = "file2", date = new DateTime(2016,05,13,12,01,00), status = "success"},
                new FileStatus(){ Id = 4, filename = "file2", date = new DateTime(2016,05,13,12,02,00), status = "fail"},
                new FileStatus(){ Id = 5, filename = "file1", date = new DateTime(2016,05,13,12,30,00), status = "success"},
                new FileStatus(){ Id = 6, filename = "file3", date = new DateTime(2016,05,13,12,31,00), status = "fail"}
            };
        }
        public DataTable PivotTable()
        {
            DataTable pivot = new DataTable();

            DateTime[] uniqueDates = filedata.Select(x => x.date.Date).Distinct().OrderBy(x => x).ToArray();

            pivot.Columns.Add("filename", typeof(string));
            foreach (DateTime date in uniqueDates)
            {
                pivot.Columns.Add(date.ToString("MM-dd-yyyy"), typeof(string));
            }

            var groups = filedata.GroupBy(x => x.filename).ToList();

            foreach (var group in groups)
            {
                DataRow newRow = pivot.Rows.Add();
                newRow["filename"] = group.Key;

                foreach (FileStatus filestatus in group)
                {
                    newRow[filestatus.date.ToString("MM-dd-yyyy")] = filestatus.status;
                }
            }
            return pivot;
        }
    }
}

答案 1 :(得分:0)

Based on your question, here is an example class that would work

public class FileStatus
{
    public int? Id { get; set; }
    public string Filename { get; set; }
    public DateTime Date { get; set; }
    public string Status { get; set; }
}

Here is a working sample that would get you the result you want, so that you can pass it onto your report template:

var filedata= new List<FileStatus>()
{
    new FileStatus(){ Id = 1, Filename = "file1", Date = new DateTime(2016,05,12,11,30,00), Status = "fail"},
    new FileStatus(){ Id = 2, Filename = "file1", Date = new DateTime(2016,05,12,11,35,00), Status = "success"},
    new FileStatus(){ Id = 3, Filename = "file2", Date = new DateTime(2016,05,13,12,01,00), Status = "success"},
    new FileStatus(){ Id = 4, Filename = "file2", Date = new DateTime(2016,05,13,12,02,00), Status = "fail"},
    new FileStatus(){ Id = 5, Filename = "file1", Date = new DateTime(2016,05,13,12,30,00), Status = "success"},
    new FileStatus(){ Id = 6, Filename = "file3", Date = new DateTime(2016,05,13,12,31,00), Status = "fail"}
};

//remove the above and replace it with your own:
//IQueryable<FileData> filedata = this.context.FileData;

var result = filedata.GroupBy(c => new { c.Filename, c.Date.Date }).Select(c => new
{
    Row = c.Key.Filename,
    Column = c.Key.Date,
    Value = filedata.FirstOrDefault(co => co.Id == c.Max(max => max.Id)).Status
}).OrderBy(c => c.Row).OrderBy(c => c.Column);

答案 2 :(得分:0)

First of all, you have to pay me for that, hahaha, all the example is wrong (repeats, year 1016¿?, hour with . instead of :, and without hour), I check you has written quickly...so:

declare @cols   nvarchar(max)
declare @query  nvarchar(max)


CREATE TABLE [dbo].[Table_1](
    [id]            [int]           NULL,
    [filename]      [nvarchar](50)  NULL,
    [date]          [datetime]      NULL,
    [status]        [nvarchar](50)  NULL
) ON [PRIMARY]

insert into table_1
values
(1     ,'file1'      ,'12-05-2016 11:30:00.000'     ,'fail'),
(2     ,'file1'      ,'12-05-2016 11:35:00.000'     ,'success'),
(3     ,'file2'      ,'13-05-2016 12:01:00.000'     ,'success'),
(3     ,'file2'      ,'13-05-2016 12:02:00.000'     ,'fail'),
(4     ,'file1'      ,'13-05-2016 12:03:00.000'     ,'success'),
(5     ,'file3'      ,'13-05-2016 12:04:00.000'     ,'fail')


select t2.filename,dat, status 
    into #temp1 
   FROM
        (select filename, cast([date] as date) as dat, max([date]) as maxdat 
            from table_1 
            group by filename, cast([date] as date)
        ) t1
   INNER JOIN
    table_1 t2
        on t1.filename = t2.filename
        and t2.[date] = t1.maxdat
        and cast(t2.date as date) = t1.dat

--select * from #temp1
--after that you want pivot by date and max status for example¿?

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), datelist, 120)) 
                    from 
                    (
                    select distinct cast(dat as date) as datelist from #temp1
                    )t1

            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
--select @cols

set @query = 
('select filename, ' + @cols + '
   from #temp1
   pivot (
        max(status)
        for dat in (' + @cols + ')
   )p
')

print @query

exec(@query)

Hope this help you! $$