To find if a specific item doesn't have any property values

Hi,

in the following code

foreach(var item in ItemList)
{
    // If an item doesn't have any property, skip it.
    continue;
    ....
}
For each item, there are several properties. However, some items don't have any values for properties. In that case I want to skip the loop for that item. The type of property is KeyValuePair[]

what is the easiest way to achieve this task?

Since I don't know what properties are there for each item, I couldn't loop through the name of properties. if(item.GetType().GetProperties(".....") != null)
IzzyTwinklyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
How have you declared the itemList object and how did you declared what it contains?
0
Daniel Van Der WerkenIndependent ConsultantCommented:
I don't know of any simple way to do this. If I were writing this code, I'd create a method that essentially does exactly what you want and run it though that method and then use the bool return to determine if I want to move forward.

This code might not be 100% spot-on, but it should compile at least. You'll need to use System.Reflection:
        private bool areAllPropertiesNull(object complexObject)
        {
            bool isNull = true;

            PropertyInfo[] properties = complexObject.GetType().GetProperties();

            if (properties != null && properties.Count() > 0)
            {
                foreach (PropertyInfo pi in properties)
                {
                    var value = pi.GetValue(complexObject);
                    if (value != null)
                    {
                        isNull = false;
                        break;
                    }
                }
            }
            return isNull;
        }

Open in new window


Once you have this method, then:

foreach(var item in ItemList)
{
    if (areAllPropertiesNull(item))
    continue;
    ....
}

Open in new window


That's what I'd do, anyway. I don't know of any built-in Framework methods that do the same thing.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
By far the easiest way is going to be by performing an equality check against a new instance that contains the properties in the state you want to validate against; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28704672
{
	class Program
	{
		static void Main(string[] args)
		{
			List<CustomClass> items = new List<CustomClass>();
			for (int i = 0; i < 20; i++)
			{
				if (i == 0)
					items.Add(new CustomClass());

				if (i % 5 == 0)
					items.Add(new CustomClass() { IntegerField = 5, DoubleField = 5, DecimalField = 5m, StringField = "FIVE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" }, { 5, "FIVE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR", "FIVE" } });

				if (i % 4 == 0)
					items.Add(new CustomClass() { IntegerField = 4, DoubleField = 4, DecimalField = 4m, StringField = "FOUR", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR" } });

				if (i % 3 == 0)
					items.Add(new CustomClass() { IntegerField = 3, DoubleField = 3, DecimalField = 3m, StringField = "THREE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE" } });

				if (i % 2 == 0)
					items.Add(new CustomClass() { IntegerField = 2, DoubleField = 2, DecimalField = 2m, StringField = "TWO", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" } }, ListField = new List<string>() { "ONE", "TWO" } });

				if (i % 1 == 0)
					items.Add(new CustomClass() { IntegerField = 1, DoubleField = 1, DecimalField = 1m, StringField = "ONE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" } }, ListField = new List<string>() { "ONE" } });
			}

			var zeros = (from item in items where item == new CustomClass() select item);
			var ones = (from item in items where item == new CustomClass() { IntegerField = 1, DoubleField = 1, DecimalField = 1m, StringField = "ONE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" } }, ListField = new List<string>() { "ONE" } } select item);
			var twos = (from item in items where item == new CustomClass() { IntegerField = 2, DoubleField = 2, DecimalField = 2m, StringField = "TWO", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" } }, ListField = new List<string>() { "ONE", "TWO" } } select item);
			var threes = (from item in items where item == new CustomClass() { IntegerField = 3, DoubleField = 3, DecimalField = 3m, StringField = "THREE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE" } } select item);
			var fours = (from item in items where item == new CustomClass() { IntegerField = 4, DoubleField = 4, DecimalField = 4m, StringField = "FOUR", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR" } } select item);
			var fives = (from item in items where item == new CustomClass() { IntegerField = 5, DoubleField = 5, DecimalField = 5m, StringField = "FIVE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" }, { 5, "FIVE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR", "FIVE" } } select item);

			Console.WriteLine("Items has {0} items", items.Count());
			Console.WriteLine("Zeros has {0} item", zeros.Count());
			Console.WriteLine("Ones has {0} items", ones.Count());
			Console.WriteLine("Twos has {0} items", twos.Count());
			Console.WriteLine("Threes has {0} items", threes.Count());
			Console.WriteLine("Fours has {0} items", fours.Count());
			Console.WriteLine("Fives has {0} items", fives.Count());
			Console.WriteLine("All collections (0 - 5) total {0} items.", (zeros.Count() + ones.Count() + twos.Count() + threes.Count() + fours.Count() + fives.Count()));
			Console.ReadLine();
		}
	}

	class CustomClass
	{
		private int fIntegerField;
		private double fDoubleField;
		private decimal fDecimalField;
		private string fStringField;
		private bool fBooleanField;
		private Dictionary<int, string> fDictionaryField;
		private List<string> fListField;

		public int IntegerField
		{
			get { return fIntegerField; }
			set { fIntegerField = value; }
		}

		public double DoubleField
		{
			get { return fDoubleField; }
			set { fDoubleField = value; }
		}

		public decimal DecimalField
		{
			get { return fDecimalField; }
			set { fDecimalField = value; }
		}

		public string StringField
		{
			get { return fStringField; }
			set { fStringField = value; }
		}

		public bool BooleanField
		{
			get { return fBooleanField; }
			set { fBooleanField = value; }
		}

		public Dictionary<int, string> DictionaryField
		{
			get
			{
				if (fDictionaryField == default(Dictionary<int, string>))
					fDictionaryField = new Dictionary<int, string>();
				return fDictionaryField;
			}
			set { fDictionaryField = value; }
		}

		public List<string> ListField
		{
			get
			{
				if (fListField == default(List<string>))
					fListField = new List<string>();
				return fListField;
			}
			set { fListField = value; }
		}

		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as CustomClass);
		}

		public bool Equals(CustomClass obj)
		{
			if (object.ReferenceEquals(obj, null))
				return false;

			return this == obj;
		}

		public override int GetHashCode()
		{
			unchecked
			{
				int hash = 17;
				hash = hash * 23 + IntegerField.GetHashCode();
				hash = hash * 23 + DoubleField.GetHashCode();
				hash = hash * 23 + DecimalField.GetHashCode();
				hash = hash * 23 + StringField.GetHashCode();
				hash = hash * 23 + BooleanField.GetHashCode();
				hash = hash * 23 + DictionaryField.GetHashCode();
				hash = hash * 23 + ListField.GetHashCode();
				return hash;
			}
		}

		public static bool operator ==(CustomClass lhs, CustomClass rhs)
		{
			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			if (object.ReferenceEquals(lhs, rhs))
				return true;

			return (lhs.IntegerField.Equals(rhs.IntegerField)) &&
				(lhs.DoubleField.Equals(rhs.DoubleField)) &&
				(lhs.DecimalField.Equals(rhs.DecimalField)) &&
				(lhs.StringField == rhs.StringField) &&
				(lhs.BooleanField.Equals(rhs.BooleanField)) &&
				((lhs.DictionaryField == rhs.DictionaryField) || lhs.DictionaryField.SequenceEqual(rhs.DictionaryField)) &&
				((lhs.ListField == rhs.ListField || lhs.ListField.SequenceEqual(rhs.ListField)));
		}

		public static bool operator !=(CustomClass lhs, CustomClass rhs)
		{
			return !(lhs == rhs);
		}
	}
}

Open in new window

Which produces the following results -Capture.JPG-saige-
0
it_saigeDeveloperCommented:
Taking it a step further, this is where static instances can help by realizing default states of the class in question, e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28704672
{
	class Program
	{
		static void Main(string[] args)
		{
			List<CustomClass> items = new List<CustomClass>();
			for (int i = 0; i < 20; i++)
			{
				if (i == 0)
					items.Add(new CustomClass());

				if (i % 5 == 0)
					items.Add(new CustomClass() { IntegerField = 5, DoubleField = 5, DecimalField = 5m, StringField = "FIVE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" }, { 5, "FIVE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR", "FIVE" } });

				if (i % 4 == 0)
					items.Add(new CustomClass() { IntegerField = 4, DoubleField = 4, DecimalField = 4m, StringField = "FOUR", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR" } });

				if (i % 3 == 0)
					items.Add(new CustomClass() { IntegerField = 3, DoubleField = 3, DecimalField = 3m, StringField = "THREE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE" } });

				if (i % 2 == 0)
					items.Add(new CustomClass() { IntegerField = 2, DoubleField = 2, DecimalField = 2m, StringField = "TWO", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" } }, ListField = new List<string>() { "ONE", "TWO" } });

				if (i % 1 == 0)
					items.Add(new CustomClass() { IntegerField = 1, DoubleField = 1, DecimalField = 1m, StringField = "ONE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" } }, ListField = new List<string>() { "ONE" } });
			}

			var zeros = (from item in items where item == new CustomClass() select item);
			var ones = (from item in items where item == CustomClass.One select item);
			var twos = (from item in items where item == CustomClass.Two select item);
			var threes = (from item in items where item == CustomClass.Three select item);
			var fours = (from item in items where item == CustomClass.Four select item);
			var fives = (from item in items where item == CustomClass.Five select item);

			Console.WriteLine("Items has {0} items", items.Count());
			Console.WriteLine("Zeros has {0} item", zeros.Count());
			Console.WriteLine("Ones has {0} items", ones.Count());
			Console.WriteLine("Twos has {0} items", twos.Count());
			Console.WriteLine("Threes has {0} items", threes.Count());
			Console.WriteLine("Fours has {0} items", fours.Count());
			Console.WriteLine("Fives has {0} items", fives.Count());
			Console.WriteLine("All collections (0 - 5) total {0} items.", (zeros.Count() + ones.Count() + twos.Count() + threes.Count() + fours.Count() + fives.Count()));
			Console.ReadLine();
		}
	}

	class CustomClass
	{
		#region Private Fields
		private Dictionary<int, string> fDictionaryField;
		private List<string> fListField;
		#endregion

		#region Public Properties
		public int IntegerField { get; set; }
		public double DoubleField { get; set; }
		public decimal DecimalField { get; set; }
		public string StringField { get; set; }
		public bool BooleanField { get; set; }

		public Dictionary<int, string> DictionaryField
		{
			get
			{
				if (fDictionaryField == default(Dictionary<int, string>))
					fDictionaryField = new Dictionary<int, string>();
				return fDictionaryField;
			}
			set { fDictionaryField = value; }
		}

		public List<string> ListField
		{
			get
			{
				if (fListField == default(List<string>))
					fListField = new List<string>();
				return fListField;
			}
			set { fListField = value; }
		}
		#endregion

		#region Static Instances
		public static CustomClass One = new CustomClass() { IntegerField = 1, DoubleField = 1, DecimalField = 1m, StringField = "ONE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" } }, ListField = new List<string>() { "ONE" } };
		public static CustomClass Two = new CustomClass() { IntegerField = 2, DoubleField = 2, DecimalField = 2m, StringField = "TWO", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" } }, ListField = new List<string>() { "ONE", "TWO" } };
		public static CustomClass Three = new CustomClass() { IntegerField = 3, DoubleField = 3, DecimalField = 3m, StringField = "THREE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE" } };
		public static CustomClass Four = new CustomClass() { IntegerField = 4, DoubleField = 4, DecimalField = 4m, StringField = "FOUR", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR" } };
		public static CustomClass Five = new CustomClass() { IntegerField = 5, DoubleField = 5, DecimalField = 5m, StringField = "FIVE", BooleanField = true, DictionaryField = new Dictionary<int, string>() { { 1, "ONE" }, { 2, "TWO" }, { 3, "THREE" }, { 4, "FOUR" }, { 5, "FIVE" } }, ListField = new List<string>() { "ONE", "TWO", "THREE", "FOUR", "FIVE" } };
		#endregion

		#region Overriden Method
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as CustomClass);
		}

		public bool Equals(CustomClass obj)
		{
			if (object.ReferenceEquals(obj, null))
				return false;

			return this == obj;
		}

		public override int GetHashCode()
		{
			unchecked
			{
				int hash = 17;
				hash = hash * 23 + IntegerField.GetHashCode();
				hash = hash * 23 + DoubleField.GetHashCode();
				hash = hash * 23 + DecimalField.GetHashCode();
				hash = hash * 23 + StringField.GetHashCode();
				hash = hash * 23 + BooleanField.GetHashCode();
				hash = hash * 23 + DictionaryField.GetHashCode();
				hash = hash * 23 + ListField.GetHashCode();
				return hash;
			}
		}

		public static bool operator ==(CustomClass lhs, CustomClass rhs)
		{
			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			if (object.ReferenceEquals(lhs, rhs))
				return true;

			return (lhs.IntegerField.Equals(rhs.IntegerField)) &&
				(lhs.DoubleField.Equals(rhs.DoubleField)) &&
				(lhs.DecimalField.Equals(rhs.DecimalField)) &&
				(lhs.StringField == rhs.StringField) &&
				(lhs.BooleanField.Equals(rhs.BooleanField)) &&
				((lhs.DictionaryField == rhs.DictionaryField) || lhs.DictionaryField.SequenceEqual(rhs.DictionaryField)) &&
				((lhs.ListField == rhs.ListField || lhs.ListField.SequenceEqual(rhs.ListField)));
		}

		public static bool operator !=(CustomClass lhs, CustomClass rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}
}

Open in new window

Which produces the same output as above.

-saige-
0
IzzyTwinklyAuthor Commented:
Thank you guys. it helped me a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.