question

Tony-6084 avatar image
0 Votes"
Tony-6084 asked DanielZhang-MSFT answered

Validation rule using LINQ

It can select operators for each user and I want to check whether the input is valid or not

Here is my expected result:

level | user | operator
1 | A | -
2 | B | AND
2 | C | AND
2 | D | -
3 | E | -

Validation rule
1. the operator must be "-" when there is 1 record in the same level
2. If there is more than 1 record in the same level, the operator must be "-" on the last record and it must select "AND"/ "OR" on other records


How to validate it using LINQ and C#?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi Tony-6084,
Please refer to following code:

 bool b= list.GroupBy(x => x.level).All(g => g.Last().operators == "-" && g.Count(x => x.operators== "-") == 1);

If it does not meet the requirements, please provide your code snippets and more details.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.