How do I filter a linq query depending on user input controls?

Posted on 2009-04-23
Last Modified: 2013-11-11
I have written a C# web site using visual studio 2008. I am using a listview to display my data. I have user input controls on my frontend listview along with a button. My idea is that upon the user entering data into my controls they click a button which will in turn return data within the listview filtered on their inputs. I have written the linq select query but I am having difficulty filtering it on my user input values. I was able to do this using textboxes containing date values (attached code) but I'm not sure how to do it using string and int values coming from dropdownlists? My attempt is attached below but no data seems to get returned?...
var query = from tt in db.Trusts

                    join cdtcd in db.TrustCrucialDates on tt.TrustID equals cdtcd.TrustID

                    join ptp in db.TrustPayments on tt.TrustID equals ptp.TrustID

                    join pdl in db.DayLookups on ptp.DayCode equals pdl.DayCode

                    join pml in db.MonthLookups on ptp.MonthCode equals pml.MonthCode

                    join pyl in db.YearLookups on ptp.YearCode equals pyl.YearCode

                    orderby cdtcd.DueDate ascending

                    select new { tt.TrustID, cdtcd.DueDate, pdl.DayDescription, pml.MonthDescription, pyl.YearDescription };

        if (this.tbSearchDateFrom.Text.Trim() != "")

            query = query.Where(x => x.DueDate >= DateTime.Parse(this.tbSearchDateFrom.Text));

        if (this.tbSearchDateTo.Text.Trim() != "")

            query = query.Where(x => x.DueDate <= DateTime.Parse(this.tbSearchDateTo.Text));

        this.lvReports.DataSource = query;


// ******* The above works fine for textboxes but the below doesn't with dropdowns ********

if (this.PaymentMonthDDL.SelectedValue.Trim() != "")

            query = query.Where(x => x.MonthDescription == this.PaymentMonthDDL.SelectedValue);

        if (this.PaymentDayDDL.SelectedValue.Trim() != "")

            query = query.Where(x => x.DayDescription == this.PaymentDayDDL.SelectedValue);

        if (this.PaymentYearDDL.SelectedValue.Trim() != "")

            query = query.Where(x => x.YearDescription.ToString() == this.PaymentYearDDL.SelectedValue);

        this.lvReports.DataSource = query;


Open in new window

Question by:Shepwedd
    LVL 9

    Expert Comment

    by:Sreedhar Vengala
    Is MonthDescription Int or String
    if String:
    if Int:
    as same with other DayDescription and Year Description
    LVL 83

    Accepted Solution

    As said above, you need to match the datatypes. The SelectedValue of ddl returns Object and in my experience LINQ is never happy comparing any other datatype such as int and string with object. If you try to compare an int to an object in a LINQ query, you will get the exception saying "No defintion was found for ....". So try to cast the SelectedValue into the type same as MonthDescription.

    Author Comment

    That worked, thanks! However, I have another linq problem. I have the attached query but it's possible that the TrustPayments table may be empty which therefore breaks my query and returns no data. Is there some way I can change my query to return data even although the TrustPayments table is empty? I was reading into left joins in linq but when I attempt to perform one (using into tw attached) I still get no data returned?

    var query = from tt in db.Trusts
                            join ttl in db.TypeLookups on tt.TypeCode equals ttl.TypeCode
                            join tfel in db.FeeExecLookups on tt.FeeExecCode equals tfel.FeeExecCode
                            join tpl in db.PartnerLookups on tt.PartnerCode equals tpl.PartnerCode
                            join cdtcd in db.TrustCrucialDates on tt.TrustID equals cdtcd.TrustID into tw
                            from cdtcd in tw.DefaultIfEmpty()
                            join cdel in db.EventLookups on cdtcd.EventCode equals cdel.EventCode
                            join ptp in db.TrustPayments on tt.TrustID equals ptp.TrustID
                            join ppfl in db.PaymentFrequencyLookups on ptp.FrequencyCode equals ppfl.FrequencyCode
                            where (tt.ClientName.Contains(propertyValue) || tt.MatterNo.Contains(propertyValue) || tt.PartnerLookup.PartnerDescription.Contains(propertyValue) || tt.FeeExecLookup.FeeExecDescription.Contains(propertyValue) || tt.TypeLookup.TypeDescription.Contains(propertyValue))
                            orderby cdtcd.DueDate ascending
                            select new { tt.TrustID, tt.ClientName, tt.MatterNo, ptp.PaymentDate, tpl.PartnerDescription, tfel.FeeExecDescription, ttl.TypeDescription, tt.TrustNotes, cdtcd.CrucialNotes, ptp.PaymentNotes, cdtcd.DueDate, cdel.EventDescription, ppfl.FrequencyDescription, ptp.Amount};

    Open in new window

    LVL 83

    Expert Comment


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now