Hi sanvi,
Refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
<td valign="top">
<asp:GridView runat="server" ID="gvCustomers1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
NorthwindEntities entity = new NorthwindEntities();
//Sub query.
var ids = entity.Customers.Where(x => x.Country == "USA").Select(x => x.CustomerID).ToList();
var query = entity.Customers.Where(x => ids.Contains(x.CustomerID)).ToList();
gvCustomers.DataSource = query;
gvCustomers.DataBind();
//Sub sub query.
var countries = entity.Customers.Where(x => x.Country == "USA").ToList();
ids = countries.Where(x => x.City != "Portland").Select(x => x.CustomerID).ToList();
query = entity.Customers.Where(x => ids.Contains(x.CustomerID)).ToList();
gvCustomers1.DataSource = query;
gvCustomers1.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 NorthwindEntities = New NorthwindEntities()
Dim ids = entity.Customers.Where(Function(x) x.Country = "USA").[Select](Function(x) x.CustomerID).ToList()
Dim query = entity.Customers.Where(Function(x) ids.Contains(x.CustomerID)).ToList()
gvCustomers.DataSource = query
gvCustomers.DataBind()
Dim countries = entity.Customers.Where(Function(x) x.Country = "USA").ToList()
ids = countries.Where(Function(x) x.City <> "Portland").[Select](Function(x) x.CustomerID).ToList()
query = entity.Customers.Where(Function(x) ids.Contains(x.CustomerID)).ToList()
gvCustomers1.DataSource = query
gvCustomers1.DataBind()
End If
End Sub
Screenshot
