How to get StringLength from DataAnnotations

After updating the offer, I still get the error message

Tried both with .SingleOrDefault() and FirstOrDefault()

enter image description here

I need to get StringLength annotation StringLength , and here is my code, but I get the following error.

I tried to implement almost the same code from here , but getting an error:

The sequence contains no elements.

  public static class DataAnnotation { public static int GetMaxLengthFromStringLengthAttribute(Type modelClass, string propertyName) { int maxLength = 0; var attribute = modelClass.GetProperties() .Where(p => p.Name == propertyName) .Single() .GetCustomAttributes(typeof(StringLengthAttribute), true) .Single() as StringLengthAttribute; if (attribute != null) maxLength = attribute.MaximumLength; return 0; } } 

// call:

 int length = DataAnnotation.GetMaxLengthFromStringLengthAttribute(typeof(EmployeeViewModel), "Name"); public class EmployeeViewModel { [StringLength(20, ErrorMessage = "Name cannot be longer than 20 characters.")] public string Name{ get; set; } } 
+5
source share
5 answers

I was able to figure it out, test it and work great if someone else was watching!

  StringLengthAttribute strLenAttr = typeof(EmployeeViewModel).GetProperty(name).GetCustomAttributes(typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().SingleOrDefault(); if (strLenAttr != null) { int maxLen = strLenAttr.MaximumLength; } 
+4
source

You can get a response from When you get the LINQ error β€œSequence contains no elements”, this usually happens because you use First () or Single (), not FirstOrDefault () and SingleOrDefault () .

So you need to try SingleOrDefault() instead of Single() as

 var attribute = modelClass.GetProperties() .Where(p => p.Name == propertyName) .SingleOrDefault() .GetCustomAttributes(typeof(StringLengthAttribute), true) .SingleOrDefault() as StringLengthAttribute; 
+1
source

Are you trying to show the length as part of the error message? If so, I believe you can just put ...

 public class EmployeeViewModel { [StringLength(20, ErrorMessage = "{0} cannot be longer than {1} characters.")] public string Name{ get; set; } } 
0
source
 var maxLength = typeof(EmployeeViewModel) .GetProperty("Name") .GetCustomAttributes<StringLengthAttribute>() .FirstOrDefault() .MaximumLength; 
0
source

Since I was just doing this, I thought I provided the LinqPad code I'm playing with. It is complete enough to work in Linqpad, just add import if you want to explore.

Notice where I comment on the bit you really need. Sometimes it was difficult for me to adapt the code without any context.

 ///The rest of the code given as context as I'm working on a T4 template to create Model classes from Datalayer classes, so the iteration is required in my case void Main() { CVH p = new CVH(); Type t = p.GetType(); foreach (PropertyInfo prop in t.GetProperties()) //Iterating through properties { foreach (var facet in prop.GetCustomAttributes()) //Yank out attributes { //The bit you need if ((Type)facet.TypeId == typeof(StringLengthAttribute)) //What type of attribute is this? { StringLengthAttribute _len = (StringLengthAttribute)facet; //Cast it to grab Maximum/MinimumLengths etc. Console.WriteLine($"My maximum field length is { _len.MaximumLength}"); //End of the bit you need Console.WriteLine(_len.MinimumLength); } } } } ///Test class with some attributes public class CVH { [StringLength(50)] [DataType(DataType.PhoneNumber)] public string phone_1 { get; set; } [Required(ErrorMessage = "id is required")] public int id { get; set; } [Required(ErrorMessage = "id is required")] public int contact_id { get; set; } } 
0
source

All Articles