vega.ynet.2k
vega.ynet.2k

Reputation: 25

Combining/Merging Boolean properties multiple Objects C#

I have got a ton of Objects, all of the same type. The only contain an int Id Property, the rest of the Properties are Boolean flags, also a ton of them.

Question: How can one combine them and create a new Object with flag[x] = true, where at least one of the objects has flag[x] = true, else flag[x] = false. (Sorry for this bad description, my English is not that good...)

Example (Pseudo-Code):

lst[0] = {0815, **true**, false, false, false}

lst[1] = {0815, false, false, **true**, false}

lst[2] = {0815, false, false, false, **true**}

shall result in

result = {0815, **true**, false, **true**, **true**}

I thought about grouping the Source using LINQ and then iterate through the groups, creating the new object manually.

Is there a better way?

Upvotes: 1

Views: 697

Answers (1)

Lou
Lou

Reputation: 457

One solution could be to iterate through your collection and check whether a true exists :

bool hasFirstPropertyTrue = false;
bool hasSecondProprertyTrue = false;
[...]
foreach(item in yourCollection)
{
hasFirstProprertyTrue |= item.FirstProperty;
hasSecondPropertyTrue |= item.SecondProperty;
[...]
}

Then hasFirstPropertyTrue, hasSecondPropertyTrue ... will be true if there is at least one true in your properties

Upvotes: 1

Related Questions