Home > other >  can i search in list with Dynamic LINQ parser
can i search in list with Dynamic LINQ parser

Time:01-27

in code page https://www.codeproject.com/Articles/355513/Invent-your-own-Dynamic-LINQ-parser first read above page i want search in list in list

var item2 = new List<List<object>>()
    {
        new List<object>{"a", 1000 },
        new List<object>{"n", 900, 1000},
    };

string s = "[0] == \"a\" ";

but dont work please help me my variable is not constant condition is dynamic and create from end user and maybe have (&& || == >= != and .....)

var pred = SimpleExpression.PredicateParser<Element>.Parse(s);

this line error in our code

CodePudding user response:

You can do something like this

var item2 = new List<List<object>>()
           {
              new List<object>{"a", 1000 },
        new List<object>{"n", 900 ,1000},
   };

foreach(List<object> list in item2)
{
    foreach(object obj in list)
    {
        if(obj == "a")
        {
            Console.WriteLine("find!");
        }
    }

}

CodePudding user response:

Not direct answer to your quetion, but you can use well known Dynamic Linq

var item2 = new List<List<object>>()
{
    new List<object>{"a", 1000 },
    new List<object>{"n", 900, 1000},
};

string s = "x => x[0] == \"a\" ";

var result = item2.AsQueryable().Where(s).ToList();

You can try it in dotnetfiddle

  • Related