Combine two Linq query results using Union in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

hello,

i have these two queries and i want to apply union operator so i can merge both result in other var merge and then binding it gridview please advice

var s=(from m in db.Messages
       where m.sender==Page.User.Identity.Name
       select new
       {
           ChatID=m.ChatID,
           sender=m.sender
       });

var r=(from m in db.Messages
      where m.recipient == Page.User.Identity.Name
      select new
      {
           ChatID=m.ChatID,
           recipient=m.recipient
      });

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi nauna,

Refer below sample.

If you use union the you will get distict record from both tables. And if use concat then you will get all record from both table means duplicate data.

HTML

<asp:GridView ID="gvConcat" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
<br />
<asp:GridView ID="gvUnion" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Namespaces

C#

using TestModel;

VB.Net

Imports TestModel

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        TestEntities entity = new TestEntities();
        var result1 = (from customer in entity.Customers
                        select new
                        {
                            CustomerId = customer.CustomerId,
                            Name = customer.Name,
                            Country = customer.Country
                        });

        var result2 = (from customerTest in entity.CustomerTests
                        select new
                        {
                            CustomerId = customerTest.CustomerId,
                            Name = customerTest.Name,
                            Country = customerTest.Country
                        });
        var concat = result1.Concat(result2);
        gvConcat.DataSource = concat;
        gvConcat.DataBind();
        var union = result1.Union(result2);
        gvUnion.DataSource = union;
        gvUnion.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim entity As TestEntities = New TestEntities()
        Dim result1 = (From customer In entity.Customers Select New With {Key .CustomerId = customer.CustomerId, Key .Name = customer.Name, Key .Country = customer.Country})
        Dim result2 = (From customerTest In entity.CustomerTests Select New With {Key .CustomerId = customerTest.CustomerId, Key .Name = customerTest.Name, Key .Country = customerTest.Country})
        Dim concat = result1.Concat(result2)
        gvConcat.DataSource = concat
        gvConcat.DataBind()
        Dim union = result1.Union(result2)
        gvUnion.DataSource = union
        gvUnion.DataBind()
    End If
End Sub

Screenshot