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?

JemHah
  • 434
  • 4
  • 16
  • 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. – Flydog57 Mar 02 '20 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.. – JemHah Mar 02 '20 at 19:50
  • 1
    This sounds like [LINQ: Passing lambda expression as parameter to be executed and returned by method](https://stackoverflow.com/q/1299534/215552), but then that second example is quite different from the first... – Heretic Monkey Mar 02 '20 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 – ColinM Mar 02 '20 at 20:03
  • i added the json for clarity – JemHah Mar 02 '20 at 20:07
  • @HereticMonkey i saw that example but it allow only 1 condition as parameter. I should be able to pass LINQ query that may contain Where, ThenBy, Contains etc..as we normally do a LINQ query – JemHah Mar 02 '20 at 20:10
  • You can't just pass a "query" around easily (other than wrapping it all up as an IQueryable or an IEnumerable). Generally, you can pass around delegates as predicates, etc. Take a look at Predicate Builder (http://www.albahari.com/nutshell/predicatebuilder.aspx) and the related tools – Flydog57 Mar 02 '20 at 20:49
  • That JSON also makes no sense in the context of the question. X, Y, Z, P, Q & R don't look like enums to me, if they are then that doesn't make a great deal of sense given their values. Downvoted as it's unclear what you're asking or hoping to achieve. – ColinM Mar 02 '20 at 21:27
  • The code is too premature to understand what you're up to. You should at least come up with something that has the potential of growing into working code. – Gert Arnold Mar 03 '20 at 08:32

1 Answers1

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

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Out of curiosity, what are you using to visualize the data in tables in your data1, data2 and data3 screenshots? – ColinM Mar 03 '20 at 09:27
  • 1
    @ColinM - It's https://www.linqpad.net. I do a majority of my coding in that now. It's worth paying for the premium edition. – Enigmativity Mar 03 '20 at 10:26