Validation rule using LINQ

Tony 26 Reputation points
2021-03-22T04:34:13.783+00:00

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#?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,237 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,631 Reputation points
    2021-03-23T06:16:43.447+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.