score:1

Accepted answer

That's should what you are looking for : (i love to use string.IsNullOrEmpty rather than cmp string.Empty)

_targetHasMissingRatings = _existingRatings.Any(er => string.IsNullOrEmpty(_targetRatings.FirstOrDefault(tr => er.RatingTypeId == tr.RatingTypeId)?.Rating));

score:1

Use IComparable so you can use OrderBy on the entire class or simply compare two instances of the class.

List<RatingDto> ordered = _existingRatings.OrderBy(x => x).ToList();

See code below :

    class RatingDto : IComparable<RatingDto>
    {
        public int RatingTypeId { get; set; }
        public string RatingType { get; set; }
        public string Rating { get; set; }

        public int CompareTo(RatingDto  other)
        {
            if (this.RatingTypeId != other.RatingTypeId)
            {
                return this.RatingTypeId.CompareTo(other.RatingTypeId);
            }
            else
            {
                return this.RatingType.CompareTo(other.RatingType);
            }
        }
    }

Code would look like this :

            List<RatingDto> _existingRatings = new List<RatingDto>(); 
            List<RatingDto> _targetRatings = new List<RatingDto>();
            Boolean _targetHasMissingRatings = false;

            foreach (var existing in _existingRatings)
            {
                foreach (var target in _targetRatings)
                {
                    if (existing == target)
                    {
                         _targetHasMissingRatings = true;
                         break;
                    }
                }
                if (_targetHasMissingRatings == true) break;
            }

Related Articles