-1

Ok, I have a List<> of objects (which the object contains multiple properties) and I would like to use a LINQ function to transform the List<> to List based on a conditional statement that compares TWO elements at a time. Is this possible?

I tried searching on google but couldn't find it.

Edit: Ok everyone, my apologize I didn't add code. No need to get angry at me :D

Here's some code

var words = apiResult
                 .Skip(1)
                 .OrderBy(x => x.BoundingPoly.Vertices[0].Y)
                 // add here a LINQ statement
                 .ToList();

apiResult is IReadOnlyList of EntityAnnotation

var apiResult = client.GetText(image);

After I ordered the list according to the Y axis I want to concat words (strings) as a sentence according to the Y axis so that the "words" list will transform into sentences.

For example, if I have three elements in the list such as

{ text: 'Hi', Y: 22 }
{ text: 'There!', Y: 22 }
{ text: 'Bye!', Y: 57 }

it will transform into

{ text: 'Hi There!', Y: 22 }
{ text: 'Bye!', Y: 57 }
burnsi
  • 6,194
  • 13
  • 17
  • 27
  • 3
    These kind of generic "is it possible to..." questions generally do not get a lot of answers. It would help if you could for example add a input data set, and a expected output set. Then other users can help you reverse engineer a solution for that. – nbokmans Jan 31 '23 at 16:43
  • Please, start questions of such kind ("is it possible?") from *example* (I have a list - sample of data here - I want to obtain - desired result - explanation of required filtration) – Dmitry Bychenko Jan 31 '23 at 16:49
  • It is possible you are looking for https://stackoverflow.com/questions/4460106/how-do-access-previous-item-in-list-using-linq... but indeed the question is very unclear... (Not even sure why I bothered to find an example since you going to delete the question anyway (and move account toward Q-ban))... – Alexei Levenkov Jan 31 '23 at 16:56
  • I have a List, where each string is a word. I want to build the List as sentences based on an IF condition that checks two elements. – Nick Blaine Jan 31 '23 at 16:59
  • It is possible; however, since we do not know which elements you compare and how you compare them, it is very difficult to give you an answer. Please add the relevant code and the relevant details to your question. – Olivier Jacot-Descombes Jan 31 '23 at 17:08
  • Ok! I added more details and code. – Nick Blaine Jan 31 '23 at 17:19
  • We were not angry. The question was simply not answerable without more information. – Olivier Jacot-Descombes Jan 31 '23 at 17:50
  • @OlivierJacot-Descombes <3 – Nick Blaine Jan 31 '23 at 17:54

2 Answers2

1

Is this what you after?

   var list = new List<ApiResult> { new ApiResult { text = "Hi", Y = 22 } , new ApiResult { text = "There !", Y = 22 } , new ApiResult { text = "Bye!", Y = 57 } };


   var result = list.GroupBy(x => x.Y).Select(x => new
    {
        text = x.Select(y => y.text),
        Y = x.Key
    });
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
0

I used a simplified model with tuples, since I do not have these EntityAnnotations. The tuples consist of a text and a Y value. You will have to adapt this to your real data model.

First, I group by the Y value. Then I order the groups by the Key which contains this Y value.

The groups are an enumeration of the grouped elements. So, for each group I select the texts and join them to form a sentence and create a tuple together with the Y value.

// Sample data using tuples.
(string text, int Y)[] apiResult = {
    ("Hi", 22),
    ("There", 22),
    ("Bye", 57),
};

var words = apiResult
    .GroupBy(a => a.Y) // Use: a.BoundingPoly.Vertices[0].Y
    .OrderBy(g => g.Key)
    .Select(g => (text: String.Join(' ', g.Select(w => w.text).ToArray()), Y: g.Key));

foreach (var word in words) {
    Console.WriteLine(word);
}

Prints:

(Hi There, 22)
(Bye, 57)
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Can you show me how I can perform an IF statement on "g" and "w"? – Nick Blaine Jan 31 '23 at 18:23
  • Look, my last question for today. Let's take the same List as in my OP, assume that the Y property is increasing by 1-3 for common properties. How would I do inside the ".Select()" a calculation that would string.Join if the Y value between the two elements is within a thresold of 1-3? – Nick Blaine Jan 31 '23 at 18:46
  • See: [How to GroupBy objects by numeric values with tolerance factor?](https://stackoverflow.com/questions/25376872/how-to-groupby-objects-by-numeric-values-with-tolerance-factor). – Olivier Jacot-Descombes Jan 31 '23 at 19:02