1

I'm trying to accomplish what I feel like should be a straightforward task, but have found it much more complicated than I expected.

Essentially, given:

public class MyObject
{
    public int A;
    public float B;
    public string C;
}

List<MyObject> objectList;

I would like to be able to read in strings something like:

"A < 1"
"B > 0.5"
"C = \"text\""

and for each of those get a List of items in objectList satisfying the requirement.

I've been working with LINQ queries like:

objectList.Where(obj => obj.A < 1)

so far, but am unable to figure out how to create queries like that with the field name.

Is there something straightforward that I am missing? Or is my whole approach here flawed?

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
user---
  • 13
  • 2
  • Try `objectList.Where(obj => obj.A < 1 && obj.B > 0.5 && obj.C == "text").ToList();`? – Selim Yildiz Feb 20 '20 at 05:53
  • 1
    if your objectList contains a list of MyObject class then it should yeild the results you expect. `var results = objectList.Where(obj => obj.A < 1 && B > 0.5 && C.Equals("text"));`. Make sure objectList is initialized and has values. – Jawad Feb 20 '20 at 05:53
  • 1
    If I have a correct notion of what you are trying to achieve, it's not straight-forward at all. You'll have to implement an interpreter that maps the text to code at runtime. That you've mistaken it as an easy problem to solve, this might arise from a flawed notion of how a C# program is executed (not necessarily, though). – Paul Kertscher Feb 20 '20 at 06:04
  • @PaulKertscher Yes, I think that's right. I think I was thinking of this all quite wrong. Is there a different way to go about this that would be more sensible? – user--- Feb 20 '20 at 06:07
  • @LordZounds I did not mean that it's not achievable or sensible, but it's not quite an easy task. Actually it's a great excercise to get to know how reflection works. I've already tasted blood and started to think about how this could be achieved, but it might take some time to figure it out, particularly since I've got other tasks to do ATM. – Paul Kertscher Feb 20 '20 at 06:12
  • @PaulKertscher Thanks for clarifying. I do see now that I actually think about it why it would be a more involved task. I guess it just seemed like a fairly common thing, so I didn't really think through what it would entail at first. I will do some more reading on the topic and come back to it after that. Thanks again. – user--- Feb 20 '20 at 06:20
  • You probably need expressions to build linq query dynamically. Some links to get you started: [From MSDN](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/how-to-use-expression-trees-to-build-dynamic-queries) [https://www.c-sharpcorner.com/UploadFile/vendettamit/create-dynamic-linq-using-expressions/](https://www.c-sharpcorner.com/UploadFile/vendettamit/create-dynamic-linq-using-expressions/) – Sandeep Feb 20 '20 at 06:10

2 Answers2

2

I thinks you can use Expression tree, which you may be create own lambda expression and pass the it Where function.

For example please check these link:

Dynamically generate LINQ queries

Or you can use System.Linq.Dynamic namespace.:

https://docs.telerik.com/data-access/developers-guide/linq-support/data-access-feature-ref-linq-support-dynamic-linq

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
1

You can apply AND operator (&&):

objectList.Where(obj => obj.A < 1 && obj.B > 0.5 && obj.C == "text").ToList();

More to read Filtering in LINQ

EDIT:

If you want to query based on string you can use System.Linq.Dynamic

objectList.Where("string predicate ..");
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
  • Sorry, maybe I wasn't clear. I'm not looking to apply several conditions at once. I would like to create these queries dynamically based on what's in the condition string. So, when the string is "A < 1", I want to get objectList.Where(obj => obj.A < 1). I'm unsure how to create a Func that tests the field specified in the string. – user--- Feb 20 '20 at 06:05
  • Now that makes sense to me, you can use `System.Linq.Dynamic`. I have updated my answer – Selim Yildiz Feb 20 '20 at 06:20