Comparing the Constant Value of Properties in C# Entity - Need Boolean Result for Entity Save

Kesava Subhash Gullapudi 30 Reputation points
2023-11-14T05:44:41.2533333+00:00
 public class BrandSaveDTO : TenantAndLEBaseEntityTimeZone
 {
     public long? brandSeq { get; set; }

     [Required(ErrorMessage = "BrandId is required")]
     public string brandId { get; set; }

     [Required(ErrorMessage = "BrandName is required")]
     public string brandName { get; set; }

     public string? shortDescription { get; set; }
 }

I have a Brand Save DTO for every time creating a brand, and I need to compare the value of the brand ID. If it is equal to the assigned value, Suppose brandId="Alexa",if its equal then only proceed to save an entity; otherwise, I need to display a message like "Entity is not matched." I need the result in a boolean expression. If it's true, I need to save the entity; otherwise, I need to display a message. Is there any library under Microsoft for my scenario?d Save DTO for everytime creating an brand i need to compare the value of brand id is it equal to what i have assigned value.., suppose (brandID="Alexa") if it is equal to assigned value then only proceed to save an entity otherwise need to display an message like Entity is not matched....., I need the result in boolean expression if its true need to save else display an message , 

Suggest is there any library under the microsoft for My scenario ...,

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
762 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,969 questions
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,095 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Kesava Subhash Gullapudi 30 Reputation points
    2023-11-14T13:26:09.1266667+00:00

    Based on my Scenario I have implemented Snippet

            public static bool ArePropertiesEqual<T>(T entity, params (string propertyName, object constantValue)[] propertyValues)
            {
                Type entityType = typeof(T);
                List<string> notMatchedProperties = new List<string>();
                Dictionary<string, object> propertyValuesDict = propertyValues.ToDictionary(pair => pair.propertyName, pair => pair.constantValue);
                foreach (var propertyValue in propertyValuesDict)
                {
                    var property = entityType.GetProperty(propertyValue.Key);
    
                    //if propertie is going to be null 
                    if (property == null)
                    {
                        throw new ArgumentException($"Property '{propertyValue.Key}' not found in type {entityType.Name}");
                    }
    
                    var entityValue = property.GetValue(entity);
                    if (!object.Equals(entityValue, propertyValue.Value))
                    {
                        StalwartError.affirm($"{entityValue} is Un-matched Value", StatusCodes.Status400BadRequest);
    
                    }
    
                }
                  
                
                return true;
            }
    
    
    

    Do implementation Guys if any issue has placed in my snippet ... :-)

    1 person found this answer helpful.
    0 comments No comments

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.