Select 在 Linq 查询 c# 中使用条件从级联 DropDownList 中的多个列表

Select from Multiple List inside cascading DropDownList using condition in Linq query c#

我需要 select 来自 来自一个名为 TrialEntities17 的数据库的一个级联 DropdownList 中不同表的不同列表Loss_Type

的值

这是我需要帮助的部分,因为我无法获得输出 return all

private List<LOSS_TYPE_MASTER> populateState()
    {
        // here code for populate state
        List<LOSS_TYPE_MASTER> all = new List<LOSS_TYPE_MASTER>();
        using (TrialEntities17 dc = new TrialEntities17())
        {
            all = dc.LOSS_TYPE_MASTER.ToList();
        }
        return all;

    }

      
    private List<string> populateCity(int Loss_Type)

    {
        using (TrialEntities17 dc = new TrialEntities17())
            // here code for populate city based on stateID
            if (Loss_Type == 1)
        {
            List<LINE_CONFIGRATOR> all = new List<LINE_CONFIGRATOR>();

                all = dc.LINE_CONFIGRATOR
                            .Where(a => (a.Line_Name == DropDownList1.SelectedValue && a.Loss_Type.Equals(Loss_Type) && a.STATION == L2.Text)).ToList();
            }

            else if (Loss_Type == 2)
        {
            List<LINE_SKILL_CONFIG> all = new List<LINE_SKILL_CONFIG>();

                all = dc.LINE_SKILL_CONFIG
                            .Where(b => (b.Line_Name == DropDownList1.SelectedValue && b.Loss_Type.Equals(Loss_Type) && b.STATION == L2.Text)).ToList();
        }
        return all;
    }

这里我有 populateState Dropdownlist,我 select Loss_Type,我需要 populateCity Dropdownlist 到 select 数据列表 Loss_Type [=47] =]ed.

我的完整cs代码如下,

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity.SqlServer;

namespace Loss_Portal
{
    public partial class _Default : Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            populateData();
        }
    }

    private void populateData()
    {

        // here code for populate data
        using (TrialEntities17 dc = new TrialEntities17())
        {
            var v = (from p in dc.Vehicle_Andon
                     where (p.Line_Name == DropDownList1.SelectedValue
                      && p.Loss_Type == 9 && p.Date_Time == Date1.Value)
                     join s in dc.LOSS_TYPE_MASTER on p.Loss_Description equals s.Loss_Description
                     select new
                     {
                         p,
                         s.Loss_Description,
                     });
            List<Vehicle_Andon> allP = new List<Vehicle_Andon>();

            foreach (var i in v)
            {
                Vehicle_Andon p = new Vehicle_Andon();
                p = i.p;
                p.Loss_Description = i.Loss_Description;
                
                allP.Add(p);
            }

            GridView1.DataSource = allP;
            GridView1.DataBind();
        }
    }

    private List<LOSS_TYPE_MASTER> populateState()
    {
        // here code for populate state
        List<LOSS_TYPE_MASTER> all = new List<LOSS_TYPE_MASTER>();
        using (TrialEntities17 dc = new TrialEntities17())
        {
            all = dc.LOSS_TYPE_MASTER.ToList();
        }
        return all;

    }

      
    private List<string> populateCity(int Loss_Type)

    {
        using (TrialEntities17 dc = new TrialEntities17())
            // here code for populate city based on stateID
            if (Loss_Type == 1)
        {
            List<LINE_CONFIGRATOR> all = new List<LINE_CONFIGRATOR>();

                all = dc.LINE_CONFIGRATOR
                            .Where(a => (a.Line_Name == DropDownList1.SelectedValue && a.Loss_Type.Equals(Loss_Type) && a.STATION == L2.Text)).ToList();
            }

            else if (Loss_Type == 2)
        {
            List<LINE_SKILL_CONFIG> all = new List<LINE_SKILL_CONFIG>();

                all = dc.LINE_SKILL_CONFIG
                            .Where(b => (b.Line_Name == DropDownList1.SelectedValue && b.Loss_Type.Equals(Loss_Type) && b.STATION == L2.Text)).ToList();
        }
        return all;
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        string loss_type = GridView1.DataKeys[e.NewEditIndex]["Loss_Type"].ToString();
        string station = GridView1.DataKeys[e.NewEditIndex]["Station"].ToString();
        
        // Edit Event
        GridView1.EditIndex = e.NewEditIndex;
        populateData();

        // Here populate data for dropdown state
        DropDownList ddLoss_DescriptionEdit = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("ddLoss_Description");
        if (ddLoss_DescriptionEdit != null)
        {
            ddLoss_DescriptionEdit.DataSource = populateState();
            ddLoss_DescriptionEdit.DataTextField = "Loss_Description";
            ddLoss_DescriptionEdit.DataValueField = "Loss_Type";
            ddLoss_DescriptionEdit.DataBind();
            ddLoss_DescriptionEdit.SelectedValue = loss_type;
        }

        string V = (GridView1.Rows[e.NewEditIndex].FindControl("txtStation1") as Label).Text.Trim();
        L2.Text = V;

        DropDownList ddReasonEdit = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("ddReason");
        if (ddReasonEdit != null)
        {
            ddReasonEdit.DataSource = populateCity(Convert.ToInt32(loss_type));
            ddReasonEdit.DataTextField = "Reason";
            ddReasonEdit.DataValueField = "Station";
            ddReasonEdit.DataBind();
            ddReasonEdit.SelectedValue = station;
        }
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        populateData();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // Here Update
        int Master_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Master_ID"].ToString());
        DropDownList ddLoss_DescriptionEdit = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddLoss_Description");
        DropDownList ddReasonEdit = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddReason");
        TextBox txtStationEdit = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtStation");

        using (TrialEntities17 dc = new TrialEntities17())
        {
            Vehicle_Andon p = dc.Vehicle_Andon.Where(a => a.Master_ID.Equals(Master_ID)).FirstOrDefault();
            if (p != null)
            {
                p.Loss_Type = Convert.ToInt32(ddLoss_DescriptionEdit.SelectedValue);
                p.Loss_Description = ddLoss_DescriptionEdit.SelectedItem.ToString();
                p.Reason = ddReasonEdit.SelectedItem.ToString();
                dc.SaveChanges();
                GridView1.EditIndex = -1;
                populateData();
            }
        }

    }

    protected void ddLoss_Description_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Populate city
        string Loss_Type = ((DropDownList)sender).SelectedValue;
        int rowIndex = GridView1.EditIndex;
        DropDownList ddReasonEdit = (DropDownList)GridView1.Rows[rowIndex].FindControl("ddReason");
        if (ddReasonEdit != null)
        {
            var v = populateCity(Convert.ToInt32(Loss_Type));
            ddReasonEdit.DataSource = v;
            ddReasonEdit.DataBind();
            if (ddReasonEdit.Items.Count > 0)
            {
                ddReasonEdit.SelectedIndex = 0;
            }
        }
    }
}
}

我是 Linq 查询的新手

Edited list as String

After new edit for Object

After edit Error for .dc and .obj

//first List
List<LINE_CONFIGRATOR> all = new List<LINE_CONFIGRATOR>(); 
//Second 
List<LINE_SKILL_CONFIG> all = new List<LINE_SKILL_CONFIG>();

你的两个列表是不同的,函数 returns 字符串列表首先你需要从 linq 查询 select 字符串列表。请尝试以下代码。

//Solutions 
  
public class ClassName
{ 
       public string Station { get; set; }
       public string Reason { get; set; }
}
private List<ClassName> populateCity(int Loss_Type)

{
     using (TrialEntities17 dc = new TrialEntities17())
     List<ClassName> obj = new List<ClassName>();
    if (Loss_Type == 1)
    {
        obj = (from p in dc.LINE_CONFIGRATOR
        where p.Line_Name == DropDownList1.SelectedValue && 
        p.Loss_Type.Equals(Loss_Type) && p.STATION == L2.Text
        select new ClassName
         {
           Station = p.Station,
           Reason = p.Reason
         }).ToList();
}
else if (Loss_Type == 2)
{
      obj = (from p in dc.LINE_SKILL_CONFIG
             where p.Line_Name == DropDownList1.SelectedValue && 
             p.Loss_Type.Equals(Loss_Type) && p.STATION == L2.Text
              select new ClassName
             {
             Station = p.Station,
             Reason = p.Reason
             }).ToList();
    
}
return obj;

}