Link to home
Start Free TrialLog in
Avatar of cefranklin
cefranklinFlag for United States of America

asked on

LINQ Selection: Why does one work but, not the other?

I have some code I am trying to develop to search my entities with some criteria.  In the code below, the commented out code does not work but, the uncommented code does.  Why?  They are both comparing strings...

The commented out code gets a TextBox.Text and splits it by a (;).  What is in the textbox is the full name of the person.  I have debugged and verified that the string after it was split is the exact same as the Person.FullName.

private void btnSearch_Click(object sender, EventArgs e)
        {
            System.Linq.IQueryable<Ticket> ticketResults = context.Tickets;
            
            if(!String.IsNullOrWhiteSpace(txtDescription.Text))
                ticketResults = ticketResults.Where<Ticket>(t => t.Description.ToUpper().Contains(txtDescription.Text.ToUpper()));
            
            if(!String.IsNullOrWhiteSpace(txtTitle.Text))
                ticketResults = ticketResults.Where<Ticket>(t => t.Title.ToUpper().Contains(txtTitle.Text.ToUpper()));

            if (!String.IsNullOrWhiteSpace(txtOpenedBy.Text))
            {
                List<System.Linq.IQueryable<Ticket>> multipleResults = new List<IQueryable<Ticket>>();
                //foreach (string person in txtOpenedBy.Text.Split(char.Parse(";")))
                //{
                //    multipleResults.Add(ticketResults.Where<Ticket>(t => t.OpenedBy.FullName == person));
                //}
                foreach (Person person in _openedBy)
                {
                    multipleResults.Add(ticketResults.Where<Ticket>(t => t.OpenedBy.FullName == person.FullName));
                }
                bool first = true;
                foreach (System.Linq.IQueryable<Ticket> myResult in multipleResults)
                {
                    if (first)
                    {
                        ticketResults = myResult;
                        first = false;
                    }
                    else
                    {
                        ticketResults = ticketResults.Union<Ticket>(myResult);
                    }
                }
            }

            ticketBindingSource.DataSource = ticketResults;
        }

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

You said:

What is in the textbox is the full name of the person.

and:

The commented out code gets a TextBox.Text and splits it by a (;).

Does this mean there is more than one name in the TextBox, or is there only one name with the parts (first and last) separated by a semi-colon?
Avatar of cefranklin

ASKER

Oh sorry. 1 or more names. Just trying it with 1 name doesn't work either.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops. That's what I forgot.  Looking at XML, Text and HTML view, it looked like there were no extra white spaces. I had thought of this but, I guess it would have been easier to add the .Trim() even though I was sure that wasn't it... lmao!