• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

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

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

  • 2
1 Solution
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
Is MonthDescription Int or String
if String:
if Int:
as same with other DayDescription and Year Description
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.
ShepweddAuthor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now