Hi nauna,
Refer below code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
NorthwindEntities entity = new NorthwindEntities();
var product = (from prod in entity.Products
select prod).ToList();
var category = (from cate in entity.Categories
where cate.CategoryID == 1 || cate.CategoryID == 2
select cate).ToList();
var result;
if (entity.Products.Any(x => x.CategoryID == 0))
{
result = (from p in entity.Products
join c in entity.Categories on p.CategoryID equals c.CategoryID
select new
{
CategoryID = p.CategoryID,
CategoryName = c.CategoryName,
ProductName = p.ProductName
});
}
else
{
result = from p in product
join c in category on p.CategoryID equals c.CategoryID
select new
{
CategoryID = p.CategoryID,
CategoryName = c.CategoryName,
ProductName = p.ProductName
};
}
GridView1.DataSource = result.ToList().Take(5);
GridView1.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 product = (From prod In entity.Products Select prod).ToList()
Dim category = (From cate In entity.Categories Where cate.CategoryID = 1 OrElse cate.CategoryID = 2 Select cate).ToList()
Dim result
If entity.Products.Any(Function(x) x.CategoryID = 0) Then
result = (From p In entity.Products Join c In entity.Categories On p.CategoryID Equals c.CategoryID Select New With {Key .CategoryID = p.CategoryID, Key .CategoryName = c.CategoryName, Key .ProductName = p.ProductName})
GridView1.DataSource = result.ToList().Take(5)
GridView1.DataBind()
Else
result = From p In product Join c In category On p.CategoryID Equals c.CategoryID Select New With {Key .CategoryID = p.CategoryID, Key .CategoryName = c.CategoryName, Key .ProductName = p.ProductName}
End If
GridView1.DataSource = result.ToList().Take(5)
GridView1.DataBind()
End If
End Sub