c#


C# take group with the same field form list


I would like to take, from a list, a group of elements which have the same field (direction field - look to code), and then take first item which occurs in list (myList order), checking if it is ok with if and take it.
Next, I want to take next direction (if exist), create a group and again take first element.
I don't know how many groups it will be at any step. I just know it will be max 4 group. How can I do this?
List <myClass> myList = allCreatedObjects;
class myClass
{
Control c;
Direction d;
}
public enum Direction
{
up, down, right, left,
}
You could use linq, for example to select right:
var rightGroup = myList.Where(c => c.d == Direction.right);
If you want to do this automatically for all the enum values, you can use a loop like:
foreach (Direction direction in Enum.GetValues(typeof(Direction)))
{
var groupList = myList.Where(c => c.d == direction);
}
This will result in a couple of variables.
You can also use linq like this:
var group = from item in myList
group item by item.d into g
select new { key = g.Key, listItems = g.ToList() };
This will result in an object, with the direction as key, and listItems filled with the items. If you want empty lists for the non-added directions you'll need to join with the direction enum.
As for a more exotic example:
var group = from item in myList
where item.d == Direction.left //some condition on item
group item by item.d into g
where g.Any(c => c.d == Direction.up) //some condition on the group
select new { key = g.Key, values = g.ToList() };
List<myClass> myList = new List<myClass>
{
new myClass(){c = new Button(), d = Direction.down },
new myClass(){c = new Button(), d = Direction.left },
new myClass(){c = new Button(), d = Direction.right },
new myClass(){c = new Button(), d = Direction.up },
new myClass(){c = new TextBox(), d = Direction.down },
new myClass(){c = new TextBox(), d = Direction.left },
new myClass(){c = new TextBox(), d = Direction.right },
new myClass(){c = new TextBox(), d = Direction.up },
};
public class myClass
{
public Control c;
public Direction d;
}
public enum Direction
{
up, down, right, left,
}
private void button1_Click(object sender, EventArgs e)
{
var groups = myList.GroupBy(my => my.d);
var firstElems = groups.Select(g => g.FirstOrDefault()).ToList();
}
I'm not sure what you want exactly. but if you want to group by direction, then in each direction check some condition, you may try as following:
var output = new Dictionary<Direction, List<myClass>>();
foreach (myClass cls in myList)
{
//check some condition based on cls properties
if (cls.c is TextBox && cls.d != Direction.down)
{
output[cls.d].Add(cls); //add it to output
}
}
//each item in output[direction] is of type List<myClass>
var upList = output[Direction.up]; //this is as List<myClass>
//var downList = output[Direction.down]; //this is as List<myClass>
//...

Related Links

EF Code First. Related Entity is not loading
Get Screen Resolution in Win10 UWP App
How to avoid duplication of event subscription?
Switching Language in C#.NET has no impact to site
Show Animated GIF in GTK# Image Widget
how to insert data in multiple tables using transaction
How to display images from ObservableCollection in mvvm
Adding class arrays to a List of class arrays in c#
Custom Authorize Attribute on asp.net mvc
create a private object for a class with private constructor in c# [duplicate]
Why C# anonymous type can't be used when declaring constants?
cannot make response.redirect work
HTTP upload progress
How to use Serilog with Unity?
How string is available without using system Namespace
Add table to existing PDF file at specific location

Categories

HOME
reactjs
spring-boot
oop
skype-for-business
indexing
algolia
microcontroller
opencv4android
readdir
grid
wavefront
subprocess
ios-universal-links
coding-style
normals
rename
activejdbc
responsive
dompdf
multibinding
bootstrap-datetimepicker
openoffice-basic
quality-center
handle
typeclass
hierarchy
ds-5
tikz
snapkit
eclipse-jdt
jsonapi-resources
machine-code
android-geofence
ragel
stochastic-process
latency
lfe
ng-lightning
contenteditable
html-entities
confluent
emacs24
stanford-nlp-server
serialversionuid
audio-fingerprinting
perldoc
jcifs
photoshop-script
openpgp.js
kendo-scheduler
reloaddata
spring-cloud-aws
timex
virtualenvwrapper
xcode7.1
airwatch
gdi
sqlxml
containstable
cascading
controlpanel
bioconductor
sdkman
tooltwist
alpha-beta-pruning
go-gin
livescript
nsmutableurlrequest
msdropdown
dateadd
banana-pi
hotlinking
rssi
node-amqp
code-complete
grunt-contrib-imagemin
jython-2.5
friendly-url
panelgrid
infinity.js
system.transactions
nsregularexpression
volatility
quit
inflate
wpml
netzke
beep
sup
mysql-connector
scsf
umfpack
parallel-python
openwrap
azure-appfabric
xslt-grouping
office-2003

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App