动态Linq从字符串值中选择

时间:2012-08-07 19:34:56

标签: asp.net linq

问题编辑,因为简单的表格不会使事情变得简单..

问题是我必须选择动态列值.. 列名来自prev page(DropDownList)=>当前页面(HiddenField)

这是我的当前代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;

public partial class ViewChart : System.Web.UI.Page
{
    BlupDataContext database = new BlupDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
            HiddenFieldWCellName.Value = Request.QueryString["WCEL_Name"];
            HiddenFieldSeries1.Value = Request.QueryString["Series1"];
            HiddenFieldSeries2.Value = Request.QueryString["Series2"];

            var q = from x in database.RSRAN079s
                    join y in database.RSRAN084s
                    on x.WCEL_Name equals y.WCEL_Name
                    where x.WCEL_Name == HiddenFieldWCellName.Value
                    select new
                    {
                        RSRAN079_ID = x.RSRAN079_ID,
                        PeriodStartTime = x.PeriodStartTime,
                        PLMN_Name = x.PLMN_Name,
                        RNC_Name = x.RNC_Name,
                        WBTS_Name = x.WBTS_Name,
                        WBTS_ID = x.WBTS_ID,
                        WCEL_Name = x.WCEL_Name,
                        WCEL_ID = x.WCEL_ID,
                        RRC_Conn_Access_Completitions = x.RRC_Conn_Access_Completitions,
                        RRC_Active_Drops_IU_FR = x.RRC_Active_Drops_IU_FR,
                        RRC_Active_Drops_IU_Fails = x.RRC_Active_Drops_IU_Fails,
                        RRC_Active_Drops_Radio_FR = x.RRC_Active_Drops_Radio_FR,
                        RRC_Active_Drops_Radio_Fails = x.RRC_Active_Drops_Radio_Fails,
                        RRC_Active_Drops_BTS_FR = x.RRC_Active_Drops_BTS_FR,
                        RRC_Active_Drops_BTS_Fails = x.RRC_Active_Drops_BTS_Fails,
                        RRC_Active_Drops_IUR_FR = x.RRC_Active_Drops_IUR_FR,
                        RRC_Active_Drops_IUR_Fails = x.RRC_Active_Drops_IUR_Fails,
                        RRC_Active_Drops_CIPH_FR = x.RRC_Active_Drops_CIPH_FR,
                        RRC_Active_Drops_CIPH_Fails = x.RRC_Active_Drops_CIPH_Fails,
                        RRC_Active_Drops_RNC_FR = x.RRC_Active_Drops_RNC_FR,
                        RRC_Active_Drops_RNC_Fails = x.RRC_Active_Drops_RNC_Fails,
                        RRC_Active_Drops_UE_FR = x.RRC_Active_Drops_UE_FR,
                        RRC_Active_Drops_UE_Fails = x.RRC_Active_Drops_UE_Fails,
                        RAB_Retainability_CS_Voice_SR = x.RAB_Retainability_CS_Voice_SR,
                        RAB_Retainability_CS_Conv_SR = x.RAB_Retainability_CS_Conv_SR,
                        RAB_Retainability_PS_SR = x.RAB_Retainability_PS_SR,
                        AMR_RAB_Setup_Access_Completitions = x.AMR_RAB_Setup_Access_Completitions,
                        AMR_RAB_Drop_Active_Drops_IF_FR = x.AMR_RAB_Drop_Active_Drops_IF_FR,
                        AMR_RAB_Drop_Active_Drops_IU_Fails = x.AMR_RAB_Drop_Active_Drops_IU_Fails,
                        AMR_RAB_Drop_Active_Drops_Radio_FR = x.AMR_RAB_Drop_Active_Drops_Radio_FR,
                        AMR_RAB_Drop_Active_Drops_Radio_Fails = x.AMR_RAB_Drop_Active_Drops_Radio_Fails,
                        AMR_RAB_Drop_Active_Drops_BTS_FR = x.AMR_RAB_Drop_Active_Drops_BTS_FR,
                        AMR_RAB_Drop_Active_Drops_BTS_Fails = x.AMR_RAB_Drop_Active_Drops_BTS_Fails,
                        AMR_RAB_Drop_Active_Drops_IUR_FR = x.AMR_RAB_Drop_Active_Drops_IUR_FR,
                        AMR_RAB_Drop_Active_Drops_IUR_Fails = x.AMR_RAB_Drop_Active_Drops_IUR_Fails,
                        AMR_RAB_Drop_Active_Drops_RNC_FR = x.AMR_RAB_Drop_Active_Drops_RNC_FR,
                        AMR_RAB_Drop_Active_Drops_RNC_Fails = x.AMR_RAB_Drop_Active_Drops_RNC_Fails,
                        AMR_RAB_Drop_Active_Drops_UE_FR = x.AMR_RAB_Drop_Active_Drops_UE_FR,
                        AMR_RAB_Drop_Active_Drops_UE_Fails = x.AMR_RAB_Drop_Active_Drops_UE_Fails,
                        CS_Conv_RAB_Setup_Access_Completitions = x.CS_Conv_RAB_Setup_Access_Completitions,
                        CS_Conv_RAB_Retainability_Completitions = x.CS_Conv_RAB_Retainability_Completitions,
                        CS_Conv_RAB_Active_Drops_IU_FR = x.CS_Conv_RAB_Active_Drops_IU_FR,
                        CS_Conv_RAB_Active_Drops__IU_Fails = x.CS_Conv_RAB_Active_Drops__IU_Fails,
                        CS_Conv_RAB_Active_Drops_Radio_FR = x.CS_Conv_RAB_Active_Drops_Radio_FR,
                        CS_Conv_RAB_Active_Drops_Radio_Fails = x.CS_Conv_RAB_Active_Drops_Radio_Fails,
                        CS_Conv_RAB_Active_Drops_BTS_FR = x.CS_Conv_RAB_Active_Drops_BTS_FR,
                        CS_Conv_RAB_Active_Drops_BTS_Fails = x.CS_Conv_RAB_Active_Drops_BTS_Fails,
                        CS_Conv_RAB_Active_Drops_IUR_FR = x.CS_Conv_RAB_Active_Drops_IUR_FR,
                        CS_Conv_RAB_Active_Drops_IUR_Fails = x.CS_Conv_RAB_Active_Drops_IUR_Fails,
                        CS_Conv_RAB_Active_Drops_RNC_FR = x.CS_Conv_RAB_Active_Drops_RNC_FR,
                        CS_Conv_RAB_Active_Drops_RNC_Fails = x.CS_Conv_RAB_Active_Drops_RNC_Fails,
                        CS_Conv_RAB_Active_Drops_UE_FR = x.CS_Conv_RAB_Active_Drops_UE_FR,
                        CS_Conv_RAB_Active_Drops_UE_Fails = x.CS_Conv_RAB_Active_Drops_UE_Fails,
                        Packet_Service_RAB_Setup_Access_Completitions = x.Packet_Service_RAB_Setup_Access_Completitions,
                        Packet_Service_RAB_Retainability_Completitions = x.Packet_Service_RAB_Retainability_Completitions,
                        Packet_Service_RAB_Active_Drops_IU_FR = x.Packet_Service_RAB_Active_Drops_IU_FR,
                        Packet_Service_RAB_Active_Drops_Fails = x.Packet_Service_RAB_Active_Drops_Fails,
                        Packet_Service_RAB_Active_Drops_Radio_FR = x.Packet_Service_RAB_Active_Drops_Radio_FR,
                        Packet_Service_RAB_Active_Drops_Radio_Fails = x.Packet_Service_RAB_Active_Drops_Radio_Fails,
                        Packet_Service_RAB_Active_Drops_BTS_FR = x.Packet_Service_RAB_Active_Drops_BTS_FR,
                        Packet_Service_RAB_Active_Drops_BTS_Fails = x.Packet_Service_RAB_Active_Drops_BTS_Fails,
                        Packet_Service_RAB_Active_Drops_IUR_FR = x.Packet_Service_RAB_Active_Drops_IUR_FR,
                        Packet_Service_RAB_Active_Drops_IUR_Fails = x.Packet_Service_RAB_Active_Drops_IUR_Fails,
                        Packet_Service_RAB_Active_Drops_RNC_FR = x.Packet_Service_RAB_Active_Drops_RNC_FR,
                        Packet_Service_RAB_Active_Drops_RNC_Fails = x.Packet_Service_RAB_Active_Drops_RNC_Fails,
                        Packet_Service_RAB_Active_Drops_UE_FR = x.Packet_Service_RAB_Active_Drops_UE_FR,
                        Packet_Service_RAB_Active_Drops_UE_Fails = x.Packet_Service_RAB_Active_Drops_UE_Fails,
                        Packet_Session_Rel_HSDSCH_EDCH = x.Packet_Session_Rel_HSDSCH_EDCH,
                        Packet_Session_Rel_HSDSCH_DCH = x.Packet_Session_Rel_HSDSCH_DCH,
                        Packet_Session_Rel_Alloc_Rel = x.Packet_Session_Rel_Alloc_Rel,
                        Packet_Session_Success_Ratio_SR = x.Packet_Session_Success_Ratio_SR,
                        Packet_Session_Drops_HSDSCH_EDCH_DR = x.Packet_Session_Drops_HSDSCH_EDCH_DR,
                        Packet_Session_Drops_HSDSCH_EDCH_Drops = x.Packet_Session_Drops_HSDSCH_EDCH_Drops,
                        Packet_Session_Drops_HSDSCH_DCH_DR = x.Packet_Session_Drops_HSDSCH_DCH_DR,
                        Packet_Session_Drops_HSDSCH_DCH_Drops = x.Packet_Session_Drops_HSDSCH_DCH_Drops,
                        Packet_Session_Drops_DCH_DR = x.Packet_Session_Drops_DCH_DR,
                        Packet_Session_Drops_DCH_Drops = x.Packet_Session_Drops_DCH_Drops,
                        Packet_Session_Drops_HSDSCH_EDCH_Other_DR = x.Packet_Session_Drops_HSDSCH_EDCH_Other_DR,
                        Packet_Session_Drops_HSDSCH_EDCH_Other_Drops = x.Packet_Session_Drops_HSDSCH_EDCH_Other_Drops,
                        Packet_Session_Drops_HSDSCH_DCH_Other_DR = x.Packet_Session_Drops_HSDSCH_DCH_Other_DR,
                        Packet_Session_Drops_HSDSCH_DCH_Other_Drops = x.Packet_Session_Drops_HSDSCH_DCH_Other_Drops,
                        Packet_Session_Drops_DCH_Other_DR = x.Packet_Session_Drops_DCH_Other_DR,
                        Packet_Session_Drops_DCH_Other_Drops = x.Packet_Session_Drops_DCH_Other_Drops,
                        HSDSCH_Alloc_Rel = x.HSDSCH_Alloc_Rel,
                        HSDPA_Retain_NW = x.HSDPA_Retain_NW,
                        HSDPA_Retain_RL_FR = x.HSDPA_Retain_RL_FR,
                        HSDPA_Retain_Non_RL_FR = x.HSDPA_Retain_Non_RL_FR,
                        HSDPA_Retain_Mobility_FR = x.HSDPA_Retain_Mobility_FR,
                        HSDPA_Retain_PRE_EMPTION_FR = x.HSDPA_Retain_PRE_EMPTION_FR,
                        HSDPA_Retain_Other_Mob_FR = x.HSDPA_Retain_Other_Mob_FR,
                        EDCH_Alloc_Rel = x.EDCH_Alloc_Rel,
                        HSUPA_Retain_NW = x.HSUPA_Retain_NW,
                        HSUPA_Retain_RL_FR = x.HSUPA_Retain_RL_FR,
                        HSUPA_Retain_EDCH_Rel_HSDSCH_SCC = x.HSUPA_Retain_EDCH_Rel_HSDSCH_SCC,
                        HSUPA_Retain_Other_FR = x.HSUPA_Retain_Other_FR,
                        HSDSCH_SCC_UE_FR = x.HSDSCH_SCC_UE_FR,
                        HSDSCH_SCC_UE_Fails = x.HSDSCH_SCC_UE_Fails,
                        HSDSCH_SCC_BTS_FR = x.HSDSCH_SCC_BTS_FR,
                        HSDSCH_SCC_BTS_Fails = x.HSDSCH_SCC_BTS_Fails,
                        HSDSCH_SCC_Trans_FR = x.HSDSCH_SCC_Trans_FR,
                        HSDSCH_SCC_Trans_Fails = x.HSDSCH_SCC_Trans_Fails,
                        HSDSCH_SCC_AC_FR = x.HSDSCH_SCC_AC_FR,
                        HSDSCH_SCC_AC_Fails = x.HSDSCH_SCC_AC_Fails,
                        HSDSCH_SCC_Other_FR = x.HSDSCH_SCC_Other_FR,
                        HSDSCH_SCC_Other_Fails = x.HSDSCH_SCC_Other_Fails,
                        R99_Alloc_Rel = x.R99_Alloc_Rel,
                        R99_Retain_NW = x.R99_Retain_NW,
                        Inter_RNC_SCC_Fail = x.Inter_RNC_SCC_Fail,
                        Inter_RNC_SCC_Drop = x.Inter_RNC_SCC_Drop,
                        RSRAN084_ID = y.RSRAN084_ID,
                        PeriodStartTime2 = y.PeriodStartTime,
                        RNC_Name2 = y.RNC_Name,
                        WBTS_Name2 = y.WBTS_Name,
                        WBTS_ID2 = y.WBTS_ID,
                        WCEL_Name2 = y.WCEL_Name,
                        WCEL_ID2 = y.WCEL_ID,
                        Cell_Availability = y.Cell_Availability,
                        Cell_Availability_Exclude = y.Cell_Availability_Exclude,
                        RRC_Set_Att = y.RRC_Set_Att,
                        RRC_Set_Acc_CR_NW = y.RRC_Set_Acc_CR_NW,
                        RRC_Set_Acc_CR_UE = y.RRC_Set_Acc_CR_UE,
                        RRC_Conn_Setup_SR = y.RRC_Conn_Setup_SR,
                        RRC_Conn_SR = y.RRC_Conn_SR,
                        RRC_Reg_Att = y.RRC_Reg_Att,
                        Regist_SR = y.Regist_SR,
                        RAB_Att_Voice = y.RAB_Att_Voice,
                        RAB_STP_and_ACC_CR_Voice = y.RAB_STP_and_ACC_CR_Voice,
                        RAB_SR_AMR_Voice = y.RAB_SR_AMR_Voice,
                        Min_per_Drop_Voice = y.Min_per_Drop_Voice,
                        CS_Serv_Dur_Voice = y.CS_Serv_Dur_Voice,
                        RAB_Att_UDI = y.RAB_Att_UDI,
                        RAB_Stp_Acc_SR = y.RAB_Stp_Acc_SR,
                        RAB_SR_UDI = y.RAB_SR_UDI,
                        Min_Per_Drop_UDI = y.Min_Per_Drop_UDI,
                        CS_Serv_Dur_UDI = y.CS_Serv_Dur_UDI,
                        PS_NRT_RAB_Att = y.PS_NRT_RAB_Att,
                        PS_NRT_RAB_CR = y.PS_NRT_RAB_CR,
                        PS_NRT_RAB_SR_NW = y.PS_NRT_RAB_SR_NW,
                        PS_NRT_RAB_SR_User = y.PS_NRT_RAB_SR_User,
                        Packet_Session_Att = y.Packet_Session_Att,
                        Packet_Session_Stp_SR = y.Packet_Session_Stp_SR,
                        RAB_Att_Stream = y.RAB_Att_Stream,
                        RAB_Stp_Acc_SR_Stream = y.RAB_Stp_Acc_SR_Stream,
                        RAB_SR_Stream = y.RAB_SR_Stream,
                        M_RAB_Att = y.M_RAB_Att,
                        M_RAB_Stp_Acc_SR = y.M_RAB_Stp_Acc_SR,
                        M_RAB_SR = y.M_RAB_SR,
                        SHO_Update_Att_RT = y.SHO_Update_Att_RT,
                        SHO_Update_Att_NRT = y.SHO_Update_Att_NRT,
                        SHO_SR_RT = y.SHO_SR_RT,
                        SHO_SR_NRT = y.SHO_SR_NRT,
                        SHO_Overhead = y.SHO_Overhead,
                        Inter_Sys_HHO_Att_RT = y.Inter_Sys_HHO_Att_RT,
                        Inter_Sys_HHO_Att_NRT = y.Inter_Sys_HHO_Att_NRT,
                        Inter_Sys_HHO_RT_SR = y.Inter_Sys_HHO_RT_SR,
                        Inter_Sys_HHOO_NRT_SR = y.Inter_Sys_HHOO_NRT_SR,
                        Inter_Sys_HHO_RT_DR = y.Inter_Sys_HHO_RT_DR,
                        Inter_Sys_HHO_NRT_DR = y.Inter_Sys_HHO_NRT_DR,
                        Inc_IS_Change_Att = y.Inc_IS_Change_Att,
                        Intra_Sys_HHO_Att = y.Intra_Sys_HHO_Att,
                        Intra_Sys_HHO_SR = y.Intra_Sys_HHO_SR,
                        R99_Allo_DL_DCH_Cap_Data_Call = y.R99_Allo_DL_DCH_Cap_Data_Call,
                        R99_Allo_UL_DCH_Cap_Data_Call = y.R99_Allo_UL_DCH_Cap_Data_Call,
                        HSDSCH_Selections = y.HSDSCH_Selections,
                        HSDPA_Res_Acc_NRT = y.HSDPA_Res_Acc_NRT,
                        HSDPA_Res_Retain_NRT = y.HSDPA_Res_Retain_NRT,
                        HSDPA_Res_Retain_RT = y.HSDPA_Res_Retain_RT,
                        HSDPA_Attempt = y.HSDPA_Attempt,
                        HSDPA_SR = y.HSDPA_SR,
                        Avg_MAC_D_THP = y.Avg_MAC_D_THP,
                        Act_HSDSCH_MAC_D_THP_NW = y.Act_HSDSCH_MAC_D_THP_NW,
                        HSDPA_DL_DATA_Rcvd_NodeB = y.HSDPA_DL_DATA_Rcvd_NodeB,
                        HSDPA_MAC_HS_Data_Vol_at_RNC = y.HSDPA_MAC_HS_Data_Vol_at_RNC,
                        HSPDA_MAC_HS_Efficiency = y.HSPDA_MAC_HS_Efficiency,
                        HARQ_Non_Ack_Ratio_Mean = y.HARQ_Non_Ack_Ratio_Mean,
                        HSDPA_SCC_Att = y.HSDPA_SCC_Att,
                        E_DCH_Selections = y.E_DCH_Selections,
                        HSUPA_Res_Acc_NRT = y.HSUPA_Res_Acc_NRT,
                        HSUPA_Res_Retain_NRT = y.HSUPA_Res_Retain_NRT,
                        HSUPA_Res_Retain_RT = y.HSUPA_Res_Retain_RT,
                        HSUPA_Att = y.HSUPA_Att,
                        HSUPA_SR = y.HSUPA_SR,
                        HSUPA_MAC_es_Data_Vol_RNC = y.HSUPA_MAC_es_Data_Vol_RNC,
                        HSUPA_MAC_es_BLER = y.HSUPA_MAC_es_BLER,
                        E_DCH_HARQ_Non_Ack_Ratio = y.E_DCH_HARQ_Non_Ack_Ratio,
                        HSUPA_SCC_Att = y.HSUPA_SCC_Att,
                        AVG_CS_Traffic = y.AVG_CS_Traffic,
                        Inc_SMS_Att = y.Inc_SMS_Att
                    };


            Chart1.DataSource = q;
            Chart1.Series[0].XValueMember = "PeriodStartTime";
            Chart1.Series[0].YValueMembers = HiddenFieldSeries1.Value.ToString();

            Chart1.Series.Add(HiddenFieldSeries2.Value);
            Chart1.Series[1].XValueMember = "PeriodstartTime2";
            Chart1.Series[1].YValueMembers = HiddenFieldSeries2.Value.ToString();
    }
}

我们可以简化Linq选择新代码吗?虽然我只需要4列.. :(

2 个答案:

答案 0 :(得分:1)

就你的评论/标签而言,你的问题没有多大意义,所以我做了最好的猜测:

DDLLessons.DataSource = lesson;
DDLLessons.DataBind();


var query = from x in db.Scores
            where x.StudentName == DDLStudentName.SelectedItem.Value
            select new {
               Day = x.Day,
               Class = DDLLessons.SelectedItem.Value
            };

答案 1 :(得分:0)

您是否正在寻找使用字符串动态选择表名(这是我所理解的)。如果是,请查看此内容并从字符串中选择表对象:

How can I dynamically select my Table at runtime with Dynamic LINQ

从上面的链接中,这是与您相关的内容:

'' get the table from a type (which corresponds to a table in your database)
Dim typeName = "DatabaseTest.AdventureWorks." & tableName & ", DatabaseTest"
Dim entityType = Type.GetType(typeName)
Dim table = dc.GetTable(entityType)
Dim query As IQueryable = table

(代码归功于我提供的链接上答案的原始海报)

更新: C#版代码

// get the table from a type (which corresponds to a table in your database)
string typeName = "DatabaseTest.AdventureWorks." + tableName + ", DatabaseTest";
Type entityType = Type.GetType(typeName);
object table = dc.GetTable(entityType);
IQueryable query = table as IQueryable;