简体   繁体   中英

Implementing dynamic query using linq functions

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;
            }
        }

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM