0

I have an enum say EnumA with values X, Y, Z, P, Q R I have created a function called Sort(linqquery as parameter) in C#, .NET version 4.7.2

I should be able to write a function parameter something like

tt=>tt.X.Ascending().ThenBy(tt.Y).ThenBy(tt.P.Descending())

The function body will sort the returning result by A ascending then by Y and then P Descending.

It is only one example of I want to achieve.

Another Example:

RemoveColumn ( LINQquery as parameter)

    tt=>tt.X && tt.Y && tt.Q

[{  "X": "Name",  "Y": "Hello",  "Z": 10,  "P": "Some value",  "Q": " Hello Again",  "R": "my data"}, {  "X": " Ha ha ha ",   "Y": "by bye",   "Z": 100,   "P": " value",   "Q": " Again",   "R": "m data"},{   "X": " Your Name",   "Y": "why",   "Z": 9,   "P": "  Ok  Some value",   "Q": " Music",   "R": " atda" }, {   "X": "John",   "Y": "Nuew",   "Z": 10,   "P": "Why your  value",   "Q": " Ta ta ata Again",   "R": "  cycle" }]

Above is my sample JSON also. Based on linq parameter i will filter or sort this JSOn before being returned by the function. How do I be able to pass LINQ as parameter?

9
  • 1
    I'm not following. An enum is effectively an integral type with symbolic aliases for each value. So, if you have enum EnumA { X, Y, Z, P, Q, R}, X is an alias for 0 while R is an alias for 5. What does this have to do with the lambda expressions you are showing. By the way "LINQ" is the name of "Language Integrated Query". What you are talking about are expressions. Commented Mar 2, 2020 at 19:43
  • X,Y Z.. are enum values but assume they are column names also for the data returned by this function. So when the data is returned by this sorthing would be based on column X, then Y etc.. Commented Mar 2, 2020 at 19:50
  • 1
    This sounds like LINQ: Passing lambda expression as parameter to be executed and returned by method, but then that second example is quite different from the first... Commented Mar 2, 2020 at 19:55
  • Can you provide some valid examples of your model and function structure? LINQquery as parameter isn't valid code. What is Ascending? Does your model contain a collection of EnumA? Neither of the code snippets are valid, nor do they have any context Commented Mar 2, 2020 at 20:03
  • i added the json for clarity Commented Mar 2, 2020 at 20:07

1 Answer 1

1

From the sounds of it you basically need to do this:

Func<IEnumerable<Data>, IEnumerable<Data>> transform =
    tt => tt.OrderBy(t => t.X).ThenBy(t => t.Y).ThenByDescending(t => t.P);

Func<Data, bool> predicate =
    t => t.X == "John" && t.Y == "Nuew" && t.Q == " Ta ta ata Again";

You'd start with the class Data:

public class Data
{
    public string X;
    public string Y;
    public int Z;
    public string P;
    public string Q;
    public string R;
}

Then read it in like this:

IEnumerable<Data> data1 = JsonConvert.DeserializeObject<Data[]>(File.ReadAllText("file.json"));

And then call the following:

IEnumerable<Data> data2 = transform(data1);

IEnumerable<Data> data3 = data2.Where(predicate);

That'll give you this:

datas

Sign up to request clarification or add additional context in comments.

2 Comments

Out of curiosity, what are you using to visualize the data in tables in your data1, data2 and data3 screenshots?
@ColinM - It's linqpad.net. I do a majority of my coding in that now. It's worth paying for the premium edition.

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.