Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Using LINQ to take two variables into a 2-dimensional array?

Tags:

c#

linq

I have a list<> of an "region" class with two variables, "startLocation" and "endLocation". I'd like to combine those two into a new sorted 2 dimensional array where its just Location and an integer representing whether its start or an end.

For example, if the list has three region objects with

[Region 1] : startLocation = 5, endLocation = 7

[Region 2] : startLocation = 3, endLocation = 5

[Region 3] : startLocation = 8, endLocation = 9

I'd like to get a sorted two dimensional array (or list or similar) looking like:

[3] [1]

[5] [1]

[5] [-1]

[7] [-1]

[8] [1]

[9] [-1]

(preferably i'd like the overlaps to add their second values together, so the two separate 5's in the array would be combined into [5 0]...but that's not too important)

I'm currently using a regular forloop going through each one by one and adding them to a list one at a time. This implementation is quite slow because I'm working with large datasets, and I'm guessing there's a more elegant / faster way to accomplish this through LINQ.

Any suggestions would be much appreciated.

like image 677
DarkAmgine Avatar asked May 05 '09 00:05

DarkAmgine


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

You'll need to define a helper method which splits a region into 2 parts and it's much easier to represent this using a new struct vs. a 2D array

struct Data {
  public int Value;
  public bool IsStart;
}

public static IEnumerable<Data> Split(this Region region) {
  yield return new Data() { Value = region.StartLocation, IsStart=true};
  yield return new Data() { Value = region.EndLocation, IsStart=false};
}

Then you can use the following LINQ query to break them up and sort them.

List<Region> list = GetTheList();
var query = list
  .SelectMany(x =>  x.Split())
  .OrderBy(x => x.Data);
like image 173
JaredPar Avatar answered Oct 10 '22 20:10

JaredPar