score:1

Accepted answer
Employees.Where(e => string.IsNullOrEmpty(e.FirstName) || string.IsNullOrEmpty(e.LastName))

score:2

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<T> (obvious interface method is bool IsSatisfiedBy(T entity)) and then

public static IEnumerable<T> GetInvalidEntities<T>(
    this IEnumerable<T> source,
    ISpecification<T> specification
) {
    return source.Where(x => !specification.IsSatisfiedBy(x));
}

For example:

public class EmployeeSpecification : ISpecification<Employee> {
    public bool IsSatisfiedBy(Employee entity) {
        Contract.Requires<ArgumentNullException>(entity != null);
        return !String.IsNullOrWhiteSpace(entity.FirstName) &&
               !String.IsNullOrWhiteSpace(entity.LastName);
    }
}

Then you can say:

// IEnumerable<Employee> employees;
// EmployeeSpecification specification;
var invalidEmployees = employees.GetInvalidEntitites(specification);

score:0

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.

score:0

If I understand the question, the solution should be pretty trivial:

  IEnumerable<Employee> collection = iQuaryable.AsEnumerable();
  List<Employee> myCollection = new List<Employee>();

  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.

score:0

Do you want to explicitly state which fields for which your method will check the objects? Or do you want all string properties checked? Or are the properties static and known at compile time? Are the objects all the same type?

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<T> HasNullStringProperties<T>(this IEnumerable<T> 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));

Related Articles