none
Logic to filter data from collection RRS feed

  • Question

  • Hi,

    I have 2 variables. 

    string Rate = "";

    string Descr = "";

    if (Rate != "" || Descr != "")
              {
                FRuleCollection _ruleColl = new FRuleCollection();
                foreach (FRule rule in _activeFeeCollection)
                {
                  if (rule.Descr.ToLower().Contains(Rate) && rule.LongDescr.ToLower().Contains(Descr))
                  {
                    _ruleColl.Add(rule);
                  }
                }
                _activeFeeCollection = _ruleColl;
              }

    I want to update this logic as it is comparing even value if it is empty. Or linq statement to filter data.

    Thank you..



    Thursday, June 6, 2019 8:05 AM

Answers

  • I want logic to filter "Rate" and "Descr" only if any one have value. But condition

    if (rule.Descr.ToLower().Contains(Rate) && rule.LongDescr.ToLower().Contains(Descr))

    filtering data using both parameter.(Remove "Rate" Or "Descr" from condition if no value)



    To remove empty Rate and Descr, maybe try this:

       if( ( Rate == “” || rule.Descr.ToLower().Contains(Rate)) &&
           ( Descr == “” || rule.LongDescr.ToLower().Contains(Descr))
    ) . . .

    Thursday, June 6, 2019 5:55 PM

All replies

  • Hi 

    Thank you for posting here.

    Could you provide the following keywords information?

    FRuleCollection, FRule ,_activeFeeCollection,

    Besides, I want to know what you want to do about this code.

    We are waiting for your update.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 6, 2019 8:17 AM
    Moderator
  • Thanks for reply.

    I want logic to filter "Rate" and "Descr" only if any one have value. But condition

    if (rule.Descr.ToLower().Contains(Rate) && rule.LongDescr.ToLower().Contains(Descr))

    filtering data using both parameter.(Remove "Rate" Or "Descr" from condition if no value)

    Thank you..

    Thursday, June 6, 2019 8:34 AM
  • https://www.tutorialspoint.com/linq/linq_lambda_expressions.htm

    var rules = (from a in _activeFeeCollection.where(x => x.Descr.ToLower().Contains(Rate) && x.LongDescr.ToLower().Contains(Descr) select a).ToList():
    
    foreach(var rule in rules)
    {
       var desc = rule.LongDescr;
    }



    • Edited by DA924x Thursday, June 6, 2019 9:24 AM
    Thursday, June 6, 2019 9:23 AM
  • Hi

    Thanks for the feedback.

    I think the following code is in contradiction.

    string Rate = "";
    
    string Descr = "";
    
    if (Rate != "" || Descr != "")

    If you set rate and descr is null, you will not enter the if statement.

    I make a simple code for your reference. I only could guess what you want to do because of not providing the key code.

    static void Main(string[] args)
            {
    
                    List<FRule> _ruleColl = new List<FRule>();
                    _ruleColl.Add(new FRule { Descr = "", LongDescr = "test1" });
                    _ruleColl.Add(new FRule { Descr = "test2", LongDescr = "test3" });
                    _ruleColl.Add(new FRule { Descr = "t1", LongDescr = "" });
                    var result = (from m in _ruleColl
                                 where m.Descr.Equals(string.Empty)|| m.LongDescr.Equals(string.Empty)
                                 select m).ToList();
                    foreach (var item in result)
                    {
                        _ruleColl.Remove(item);
                    }
    
                
            }
            public class FRule
            {
                public string Descr { get; set; }
    
                public string LongDescr { get; set; }
            }

    This code will delete the item that Rate or Descr has no value.

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, June 6, 2019 9:26 AM
    Moderator
  • I want logic to filter "Rate" and "Descr" only if any one have value. But condition

    if (rule.Descr.ToLower().Contains(Rate) && rule.LongDescr.ToLower().Contains(Descr))

    filtering data using both parameter.(Remove "Rate" Or "Descr" from condition if no value)



    To remove empty Rate and Descr, maybe try this:

       if( ( Rate == “” || rule.Descr.ToLower().Contains(Rate)) &&
           ( Descr == “” || rule.LongDescr.ToLower().Contains(Descr))
    ) . . .

    Thursday, June 6, 2019 5:55 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,
    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 7:22 AM
    Moderator