2017-09-22 22 views
1

我有一个表如下:Linq如何可以通过变量动态选择一个表格的字段?

public class Report 
{ 

     public int ReportId { get; set; } 

     public string ReportItem { get; set; } 

     public int RowN { get; set; } 

     public decimal M00 { get; set; } 

     public decimal M01 { get; set; } 

     public decimal M02 { get; set; } 

     public decimal M03 { get; set; } 

     public decimal M04 { get; set; } 

     public decimal M05 { get; set; } 

     public decimal M06 { get; set; } 

     public decimal M07 { get; set; } 

     public decimal M08 { get; set; } 

     public decimal M09 { get; set; } 

     public decimal M10 { get; set; } 

     public decimal M11 { get; set; } 

     public decimal M12 { get; set; } 
} 

每次第一4个字段应该被视为和要观看的第五字段是从剩余的12个字段中选择的一个。所选变量从视图页面传输到控制器,然后通过ViewBage返回到视图页面。我已经通过jQuery Datatable解决了这个问题。键代码行如下所示:

<script type="text/javascript"> 
     $(document).ready(function() 
     { 
      var table = $('.table').dataTable(); 

      for (var i=4; i <= 15; ++ i) 
      { 
       table.fnSetColumnVis(i, false, false); 
      } 

      var i = @ViewBag.strMon; 

      table.fnSetColumnVis(i, true, false); 

     }); 

</script> 

在方式中,所有的12个字段的首先HIDEN,然后将它们中的一个被所述可变选择来显示。

但它是一个令人满意的方式,每次当我提出一个新的选择,所有的12场都显示在视图页面,然后11S慢慢消失。

我还有另外一个想法来解决这个问题。我做了另一个表如下:

公共类ReportView {

public int ReportId { get; set; } 

    public string ReportItem { get; set; } 

    public int RowN { get; set; } 

    public decimal M00 { get; set; } 

    public decimal M99 { get; set; } 

} 

我想要做的就是写从报告中的数据在控制器中ReportView然后进行与ReportView相关视图页面显示。但我不知道该怎么做。如果表格字段可以由变量选择,问题将被解决,或者表格可以通过列号返回。任何帮助?谢谢。

+0

你可以将问题简化得更清楚吗? – DiskJunky

+0

感谢您的关注。为了简化,表格中有16个字段(或列),但每次只查看5个字段。前4个总是被看到,并且第5个被从12s变量选择。 viewModel的表二是建立的,其中只有5个字段。如何通过变量的选项来读取表1到表2中的数据。 – yMvcBeginner

+0

有人建议编写一个存储过程如下: – yMvcBeginner

回答

0

对于我的问题,我无法得到满意的答案。我认为我没有完全理解自己。现在我通过使用存储过程解决了这个问题。我希望这有助于明确我的问题,也可以帮助其他人解决同样的问题。程序如下:

USE [MvcAccDb] 
GO 

/****** Object: StoredProcedure [dbo].[InsertDataIntoReportViews] Script Date: 09/24/2017 19:13:23 ******/ 

SET ANSI_NULLS ON 

GO 
SET QUOTED_IDENTIFIER ON 
GO 

-- ============================================= 
-- Author:  <Author,,Name> 
-- Create date: <Create Date,,> 
-- Description: <Description,,> 
-- ============================================= 

ALTER PROCEDURE [dbo].[InsertDataIntoReportViews] 
    -- Add the parameters for the stored procedure here 
    (@month nvarchar(2)) 

AS 

BEGIN 

    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 


    -- Insert statements for procedure here 

    insert into ReportViews (ReportID, ReportItem, RowN, M00, M99) 
    select [ReportID], 
    [ReportItem], 
    [RowN], 
    [M00], 
    CASE 
    WHEN @month = '01' then M01 
    WHEN @month = '02' then M02 
    WHEN @month = '03' then M03 
    WHEN @month = '04' then M04 
    WHEN @month = '05' then M05 
    WHEN @month = '06' then M06 
    WHEN @month = '07' then M07 
    WHEN @month = '08' then M08 
    WHEN @month = '09' then M09 
    WHEN @month = '10' then M10 
    WHEN @month = '11' then M11 
    WHEN @month = '12' then M12 
    END AS M99  
    from Reports 
    END