Iterating through collection items and checking each property for a valid value?

For simplicity...." This works in a very specific case and gives no indication how to get started on approaching the more general problem. – Jason Nov 10 '10 at 21:52 @Jason, I would strongly prefer writing the validation checks statically in code over a more generic approach that specifies the conditions in some other form. Either way you're encoding the checks in some way and writing it as straightforward code in a well-defined business layer is more maintainable than a more round-about architecture.

– Dan Bryant Nov 11 '10 at 16:54 @Dan Bryant: Disagree. Specifications are reusable, embedded validation checks are not. Decoupling the validation checks from the validation mechanism is more, not less, maintainable than what you propose.

– Jason Nov 11 '10 at 18:43.

This should get you started: var properties = typeof(Employee).GetProperties() . Where(p => p. PropertyType == typeof(string)); foreach(var employee in employees) { foreach(var property in properties) { string value = (string)property.

GetValue(employee, null); if(String. IsNullOrWhiteSpace(value)) { yield return employee; break; } } } It's clear how to generalize this using generics. Even better is to just have a concrete class that implements some interface ISpecification (obvious interface method is bool IsSatisfiedBy(T entity)) and then public static IEnumerable GetInvalidEntities( this IEnumerable source, ISpecification specification ) { return source.

Where(x =>!specification. IsSatisfiedBy(x)); } For example: public class EmployeeSpecification : ISpecification { public bool IsSatisfiedBy(Employee entity) { Contract. Requires(entity!

= null); return!String. IsNullOrWhiteSpace(entity. FirstName) &&!String.

IsNullOrWhiteSpace(entity. LastName); } } Then you can say: // IEnumerable employees; // EmployeeSpecification specification; var invalidEmployees = employees. GetInvalidEntitites(specification).

I think the question is about only two properties (FirstName and LastName) and should cater for empty strings – Jakub Konecki Nov 10 '10 at 21:47 @Jakub Konecki: No, Employee is just an example. I think he's looking for something more general. – Jason Nov 10 '10 at 21:50 Oh, I see.

Then reflection is a good way - You might want to extend your first code sample with the check for property type (string) and empty strings as values – Jakub Konecki Nov 10 '10 at 22:02 or Func instead of ISpecification – Jakub Konecki Nov 10 '10 at 6:35.

Simply create a second collection "invalidEmployees" Iterate through Employee collection. Check current Employee for null values (or any other characteristic) then add them to the "invalidEmployees" collection.

If I understand the question, the solution should be pretty trivial: IEnumerable collection = iQuaryable.AsEnumerable(); List myCollection = new List(); foreach(var emp in collection) { if(string. IsNullOrEmpty(emp. LastName) || string.

IsNullOrEmpty(emp. FirstName)) myCollection. Add(emp); } where the iQuaryable is the instance of your IQuaryable implementation.

It could be as simple as a where clause like shown in other answers: var nullPropItems = objectCollection. Where(ob => string. IsNullOrEmpty(ob.

Property)); Or it could be as complex as specifying the string property names you want to check and iterating them: public static IEnumerable HasNullStringProperties(this IEnumerable items, params string propertyNames) { var properties = items.GetType(). GetGenericArguments()0.GetProperties(); foreach(var item in items) foreach(string propertyName in propertyNames) { var propertyInfo = properties. SingleOrDefault(p => p.Name.

Equals(propertyName)); if (null! = propertyInfo) { if (string. IsNullOrEmpty(propertyInfo.

GetValue(item, null) as string)) yield return item; } } } Callable with something like: var items = objectCollection. HasNullStringProperties("FirstName", "LastName", "OrganizationName"); You could further cut down on the number of properties you check against if you pre-filter for string types. After the GetProperties() add .

Where(prop => prop.GetType() == typeof(string)).

Var nullPropItems = objectCollection. Where(ob => string. Var properties = items.GetType().

Var propertyInfo = properties. SingleOrDefault(p => p.Name. Var items = objectCollection.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions