Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using one dropdownlist to show product stock by selecting particular category from dropdownlist.
There are 2 categories for product. I am adding one more option for All.
Then how to create the LINQ Query for All.

ASP.NET
<asp:DropDownList ID="drpProdType" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpProdType_SelectedIndexChanged" CssClass="field-long">
                    <asp:ListItem Value="0">--Select--</asp:ListItem>
                    <asp:ListItem Value="1">JoiningProduct</asp:ListItem>
                    <asp:ListItem Value="2">RepurchaseProduct</asp:ListItem>
                    <asp:ListItem Value="3">ALL</asp:ListItem>
                </asp:DropDownList>


This is dropdownlist and below is my LINQ Query. But its not showing all records when I select All Option from dropdownlist.
C#
var prodList = (from l in db.tbl_ProductTbls
                            where l.tbl_ProductType.Type == drpProdType.SelectedItem.Text || l.tbl_ProductType.Type.Equals(string.Empty)
                            select l).ToList();


What I have tried:

var prodList = (from l in db.tbl_ProductTbls
where l.tbl_ProductType.Type == drpProdType.SelectedItem.Text || l.tbl_ProductType.Type.Equals(string.Empty)
select l).ToList();
Posted
Updated 11-Mar-16 1:18am

1 solution

Try something like this:
C#
var products = db.tbl_ProductTbls.AsQueryable();
if (drpProdType.SelectedValue != "3")
{
    products = products.Where(l => l.tbl_ProductType.Type == drpProdType.SelectedItem.Text || l.tbl_ProductType.Type.Equals(string.Empty));
}

var prodList = products.ToList();
 
Share this answer
 
v2
Comments
Veeshal Mali 11-Mar-16 7:47am    
thank you sir....
But It's showing an error
Cannot Implicitly convert System.Data.Linq.IQueryable<tbl_productTbl> to System.Data.Linq.Table<tbl_productTbl>
I cast it like this
(System.Data.Linq.Table<tbl_ProductTbl>)
then it rises exception
Unable to cast object of type 'System.Data.Linq.DataQuery`1[tbl_ProductTbl]' to type 'System.Data.Linq.Table`1[tbl_ProductTbl]'.
then How to cast it Sir ?
Richard Deeming 11-Mar-16 7:48am    
Try with an .AsQueryable() on the first line:
var products = db.tbl_ProductTbls.AsQueryable();
Veeshal Mali 11-Mar-16 7:56am    
thank you sir....
Its working....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900