Hi nauna,
Check this sample now take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView runat="server" ID="gvCustomers">
</asp:GridView>
Namespaces
C#
using System.Linq;
using CustomerModel;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (NorthwindEntities entities = new NorthwindEntities())
{
var result = (from customer in entities.Customers
select new
{
id = customer.CustomerID,
name = customer.ContactName,
country = customer.Country
});
var result1 = (from customer in entities.Customers
select new
{
id = customer.CustomerID,
name = customer.ContactName,
country = customer.Country
});
foreach (var res1 in result1)
{
// Checking if id already exist or not.
if (result.Where(x => x.id == res1.id).Count() < 0)
{
// Add if not exist.
result.ToList().Add(new { id = res1.id, name = res1.name, country = res1.country });
}
}
gvCustomers.DataSource = result;
gvCustomers.DataBind();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Using entities As NorthwindEntities = New NorthwindEntities()
Dim result = (From customer In entities.Customers Select New With {Key
.id = customer.CustomerID, Key
.name = customer.ContactName, Key
.country = customer.Country
})
Dim result1 = (From customer In entities.Customers Select New With {Key
.id = customer.CustomerID, Key
.name = customer.ContactName, Key
.country = customer.Country
})
For Each res1 In result1
If result.Where(Function(x) x.id = res1.id).Count() < 0 Then
result.ToList().Add(New With {Key
.id = res1.id, Key
.name = res1.name, Key
.country = res1.country
})
End If
Next
gvCustomers.DataSource = result
gvCustomers.DataBind()
End Using
End If
End Sub