0

I am trying to implement a block of code which can Dynamically add a where clause to a linq query. It totally helps me to consider only full text boxes during my search process and jump over empty controls.Now the problem is, Despite all conditions which i have added to my Linq query, It returns all rows and ignores where clauses.

what is the reason and how can i avoid this problem ?

Here i have a simple abstract BaseEntity class which is contained only one field called Id :

public abstract class BaseEntity
    {
        public virtual int Id { get; set; }
    }

My EmailEntity class is inherited from this BaseEntity :

    public class EmailEntity : BaseEntity
    {
        public string Address { get; set; }
        public string Pwd { get; set; }
        public string Hint { get; set; }
        public EmailGroup? EmailGroup { get; set; }
    }

This is where i create my query :

 private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            var mails = new List<EmailEntity>();    
            using (var op = new OperationContext())
            {
                var query = op.EmailEntities;
                if (!string.IsNullOrEmpty(SearchEmailTxt.Text))
                    query.Where(item => item.Address == SearchEmailTxt.Text);
                if (!string.IsNullOrEmpty(SearchEmailIdTxt.Text))
                    query.Where(item => item.Id == Convert.ToInt16(SearchEmailIdTxt.Text));
                if (SearchEmailGroupCombo.SelectedItem != null)
                    query.Where(item => item.EmailGroup.ToString() == SearchEmailGroupCombo.SelectedItem.ToString());
                var result=query.ToList();
                result.ForEach(mails.Add);
                EmailDataGrid.ItemsSource = mails;
            }
        }

1 Answer 1

4

Because query.Where returns query back and you ignore the result, which brings you to begining. You should change your code like query = query.Where(....);

private void BtnSearch_Click(object sender, RoutedEventArgs e)
{
    var mails = new List<EmailEntity>();    
    using (var op = new OperationContext())
    {
        IQueryable<EmailEntity> query = op.EmailEntities;
        if (!string.IsNullOrEmpty(SearchEmailTxt.Text))
            query = query.Where(item => item.Address == SearchEmailTxt.Text);
        if (!string.IsNullOrEmpty(SearchEmailIdTxt.Text))
            query = query.Where(item => item.Id == Convert.ToInt16(SearchEmailIdTxt.Text));
        if (SearchEmailGroupCombo.SelectedItem != null)
            query = query.Where(item => item.EmailGroup.ToString() == SearchEmailGroupCombo.SelectedItem.ToString());
        var result=query.ToList();
        result.ForEach(mails.Add);
        EmailDataGrid.ItemsSource = mails;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't work. Now I have a compile time error says : Cannot implicitly convert type 'System.Linq.IQueryable<NB.Entity.EmailEntity>' to 'System.Data.Entity.DbSet<NB.Entity.EmailEntity>'.
@T.shaped try using either var query = op.EmailEntities.AsQueryable(); or IQueryable<EmailEntity> query = op.EmailEntities;
@AdilMammadov You have my vote, but why don't you update the answer with one of those :)
@IvanStoev, you are right, answer must have been updated for future users. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.