Add multiple Where condition in Linq query using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

Compare Two DataTables and copy Rows in Third DataTable with status using C# and VB.Net

i have asked qus few days ago. i got solution but now i have to change the condition, i have to include and operator with more filters. so please let me know, how i can add other filters with Department. Just assume i have to check with department and name and marks. 

List<DataRow> rows = dt2.AsEnumerable()
                     .Where(x => !dt1.AsEnumerable()
                     .Select(y => y.Field<string>("Department"))
                     .Contains(x.Field<string>("Department"))).ToList();
Results 1 - 5 of 6 12
Posted one month ago

I will get back to you soon.


Posted one month ago Modified on one month ago

Hey yogesjoshi,

Please refer below sample.

HTML

<table>
    <tr>
        <td>
            <asp:GridView ID="GridView1" runat="server" />
        </td>
        <td>
            <asp:GridView ID="GridView2" runat="server" />
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <asp:GridView ID="GridView3" Caption="<b>Output</b>" runat="server" />
        </td>
    </tr>
</table>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Department");
    dt.Columns.Add("name");
    dt.Columns.Add("marks");
    dt.Rows.Add("it", "abc", "80");
    dt.Rows.Add("cs", "xyz", "67");
    dt.Rows.Add("mec", "abc", "76");
    dt.Rows.Add("ece", "xyz", "73");
    dt.Rows.Add("it", "ab", "85");

    DataTable dt2 = new DataTable();
    dt2.Columns.Add("Department");
    dt2.Columns.Add("name");
    dt2.Columns.Add("marks");
    dt2.Rows.Add("it", "abc", "80");
    dt2.Rows.Add("cs", "xyz", "50");
    dt2.Rows.Add("mec", "abc", "76");
    dt2.Rows.Add("eee", "abc", "40");

    //Multiple Condition
    var dt1result = dt.AsEnumerable();
    var dt2result = dt2.AsEnumerable();
    var result = (from dt1Row in dt1result
                    from dt2Row in dt2result
                    where dt1Row["name"] == dt2Row["name"]
                            && dt1Row["Department"] == dt2Row["Department"]
                            && Convert.ToInt32(dt1Row["marks"]) == Convert.ToInt32(dt2Row["marks"])
                    select new
                    {
                        name = dt1Row.Field<string>("name"),
                        marks = dt1Row.Field<string>("marks"),
                        department = dt2Row.Field<string>("Department")
                    });

    GridView1.DataSource = dt;
    GridView1.DataBind();
    GridView2.DataSource = dt2;
    GridView2.DataBind();
    GridView3.DataSource = result;
    GridView3.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim dt As DataTable = New DataTable()
    dt.Columns.Add("Department")
    dt.Columns.Add("name")
    dt.Columns.Add("marks")
    dt.Rows.Add("it", "abc", "80")
    dt.Rows.Add("cs", "xyz", "67")
    dt.Rows.Add("mec", "abc", "76")
    dt.Rows.Add("ece", "xyz", "73")
    dt.Rows.Add("it", "ab", "85")
    Dim dt2 As DataTable = New DataTable()
    dt2.Columns.Add("Department")
    dt2.Columns.Add("name")
    dt2.Columns.Add("marks")
    dt2.Rows.Add("it", "abc", "80")
    dt2.Rows.Add("cs", "xyz", "50")
    dt2.Rows.Add("mec", "abc", "76")
    dt2.Rows.Add("eee", "abc", "40")
    Dim dt1result = dt.AsEnumerable()
    Dim dt2result = dt2.AsEnumerable()
    Dim result = (From dt1Row In dt1result From dt2Row In dt2result Where dt1Row("name") = dt2Row("name") AndAlso dt1Row("Department") = dt2Row("Department") AndAlso Convert.ToInt32(dt1Row("marks")) = Convert.ToInt32(dt2Row("marks")) Select New With {Key .name = dt1Row.Field(Of String)("name"), Key .marks = dt1Row.Field(Of String)("marks"), Key .department = dt2Row.Field(Of String)("Department")})
    GridView1.DataSource = dt
    GridView1.DataBind()
    GridView2.DataSource = dt2
    GridView2.DataBind()
    GridView3.DataSource = result
    GridView3.DataBind()
End Sub

Screenshot


Posted one month ago

thanks for reply, but in my example there is two datatable. comparing one with second with some keys.


Posted one month ago

Hey yogesjoshi,

This is an example how to use multiple column in where condition.

So please refer provided code and implement it in your code.


Posted one month ago
pandeyism says:

Hey yogesjoshi,

This is an example how to use multiple column in where condition.

So please refer provided code and implement it in your code.

 i have already got solution with one table with multiple condition. i wants to know my problem solution. is there any other way to do this.

thanks in advance.