Multiple Where conditions in Entity Framework C# .Net

Last Reply on Sep 15, 2014 04:46 AM By mosin

Posted on Sep 15, 2014 04:38 AM

i want to get one record
with multi condition
for example username == "me" and age grather than 20
please help me

Posted on Sep 15, 2014 04:46 AM Modified on on Sep 15, 2014 06:54 AM

I have created sample demo using entity framework for searching employees based on employee name (first name or last name) and city.

1. Make a connection string for sql server database in web.confing.

2. In System folder App_Code create new folder name LINQ.

3. In LINQ folder add your .dbml file.

4. Then using server connection of your database drags and drops your table. e.g. Employees

5. Use namespace in your file i.e.using LINQ and using System.Linq

6. Write a linq query using data context for search criteria.

Please refer the following article for entity framework:

HTML

<div>
    Employee Name :<asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
    City :<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="SearchEmployees" />
    <br />
    <br />
    <asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="FirstName" HeaderText="First Name" ItemStyle-HorizontalAlign="Left" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" ItemStyle-HorizontalAlign="Left" />
            <asp:BoundField DataField="City" HeaderText="City" ItemStyle-HorizontalAlign="Left" />
        </Columns>
        <EmptyDataTemplate>
            <table cellspacing="0" rules="all" border="1" style="border-collapse: collapse;">
                <tr>
                    <th scope="col">
                        Employee Id
                    </th>
                    <th scope="col">
                        First Name
                    </th>
                    <th scope="col">
                        Last Name
                    </th>
                    <th scope="col">
                        City
                    </th>
                </tr>
                <tr>
                    <td colspan="4" align="center">
                        No Records Found
                    </td>
                </tr>
        </EmptyDataTemplate>
    </asp:GridView>
</div>

Namespace

using LINQ;
using System.Linq;

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.PopulateEmployees("", "");
    }
}

protected void SearchEmployees(object sender, EventArgs e)
{
    this.PopulateEmployees(this.txtEmployeeName.Text.Trim(), this.txtCity.Text.Trim());
}

public void PopulateEmployees(string employeeName, string city)
{
    using (DataClassesDataContext dc = new DataClassesDataContext())
    {
        var res = from e in dc.Employees
                    where (e.FirstName.StartsWith(employeeName) || e.LastName.StartsWith(employeeName))
                    && e.City.StartsWith(city)
                    select e;

        gvEmployees.DataSource = res.ToList();
        gvEmployees.DataBind();
    }
}

Screenshots

Table structure of Employee which is drag and drop in .dbml file from Server Explorer

Search Criteria for Employee Name and City

Solution explorer structure for entity framework