locked
if /else clause in linq lambda exp RRS feed

  • Question

  • User1378291314 posted

    Hi everybody,
    i have a MEMBERS sql table. In members.aspx page contains a dropdownlist. The dropdownlist include memberType. First item in the dropdownlist "Please select an item".

    Now if a user doesn't change the dropdownlist default item, list all members load a gridview.

    MEMBER[] m = (from x in db.MEMBERs select x).ToArray();GridView1.DataSource = m;
    GridView1.DataBind();

    But the user change the dropdownlist default item, how to i write if /else clause in linq lambda exp
    List<MEMBER> m = new List<MEMBER>();

    m.where(????? if /else clause );

    Thanks...

    Monday, August 10, 2015 10:23 AM

Answers

  • User-271186128 posted

    Hi davilanka,

    As for this issue, I suggest you could get all of the item first, then, according to the DropDownList value to filter the result from the previous result.

    You could refer to the following code:

        <div>
            <asp:DropDownList ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server"></asp:DropDownList><br />
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        </div>

    Code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    //Bind DropDownList
                    using (TestEntities context = new TestEntities())
                    {
                        var query = (from ee in context.Employees
                                    select new
                                    {
                                        City = ee.City,
                                    });
    
                        DropDownList1.DataSource = query.Distinct().ToList();
                        DropDownList1.DataTextField = "City";
                        DropDownList1.DataValueField = "City";
                        DropDownList1.DataBind();
                        DropDownList1.Items.Insert(0, new ListItem("Please select", "0"));
                    }
                    BindGridView();
                }
            }
    
            private void BindGridView()
            {
                using (TestEntities context = new TestEntities())
                {
                    var query = context.Employees.Select(c => new { EmployeeID = c.EmployeeID, FirstName = c.FirstName, LastName = c.LastName, City = c.City });
                    if (DropDownList1.SelectedValue != "0")
                    {
                        query = query.Where(dd => dd.City == DropDownList1.SelectedItem.Text);
                    }
                    GridView1.DataSource = query.ToList();
                    GridView1.DataBind();
                }
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                BindGridView();
            }

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2015 2:18 AM