Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Query any collection

Query any collection

Scheduled Pinned Locked Moved LINQ
database
6 Posts 3 Posters 19 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #1

    Some of my master tables have a boolean field IsDefault. I want to write a generic method that will accept a collection and return the item where IsDefault is true. This obviously does not work so I'm looking for ideas...

    public static object GetDefault(IEnumerable lCollection)
    {
    object oValue = null;
    if (lCollection != null)
    {
    //check that there is a default field
    PropertyInfo oProp = lCollection.First().GetType().GetProperty("IsDefault", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

    	if (oProp == null)
    	{
    		//get the item where IsDefault is true
    		oValue = lCollection.Where(x => x.IsDefault == true).FirstOrDefault();
    	}
    }
    return oValue;
    

    }

    Never underestimate the power of human stupidity RAH

    W Richard DeemingR 2 Replies Last reply
    0
    • M Mycroft Holmes

      Some of my master tables have a boolean field IsDefault. I want to write a generic method that will accept a collection and return the item where IsDefault is true. This obviously does not work so I'm looking for ideas...

      public static object GetDefault(IEnumerable lCollection)
      {
      object oValue = null;
      if (lCollection != null)
      {
      //check that there is a default field
      PropertyInfo oProp = lCollection.First().GetType().GetProperty("IsDefault", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

      	if (oProp == null)
      	{
      		//get the item where IsDefault is true
      		oValue = lCollection.Where(x => x.IsDefault == true).FirstOrDefault();
      	}
      }
      return oValue;
      

      }

      Never underestimate the power of human stupidity RAH

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Not sure how you want to use it but if you want to use the IsDefault in a LINQ query, consider something like this:

      /// /// One class
      ///
      public class A {
      public int a1 { get; set; }
      public bool IsDefault { get; set; }
      }

      /// /// Another class
      ///
      public class B {
      public int b1 { get; set; }
      public bool IsDefault { get; set; }
      }

      /// /// Helper to investigate value of one item
      ///
      public static class OHelper {

        public static bool GetDefaultValue(this object obj) {
           bool retValue = false;
      
           //check that there is a default field
           System.Reflection.PropertyInfo oProp = obj.GetType().GetProperty("IsDefault", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase);
      
           if (oProp != null) {
              if ((bool)oProp.GetValue(obj) == true) {
                 retValue = true;
              }
           }
           return retValue;
        }
      

      }

      And to use

           System.Collections.Generic.List<A> AList = new System.Collections.Generic.List<A>();
           System.Collections.Generic.List<B> BList = new System.Collections.Generic.List<B>();
      
           AList.Add( new A() { a1 = 1, IsDefault = true});
           AList.Add( new A() { a1 = 2, IsDefault = false});
      
           BList.Add( new B() { b1 = 1, IsDefault = false});
           BList.Add( new B() { b1 = 2, IsDefault = true});
      
           var query1 = from item in AList
                        where item.GetDefaultValue() == true
                        select item;
      
           foreach (A aItem in query1) { }
      
           var query2 = from item in BList
                        where item.GetDefaultValue() == true
                        select item;
      
           foreach (B bItem in query2) { }
      

      Of course this could be simpler if both of the classes have for example common interface which would define the existence of IsDefault property.

      M 1 Reply Last reply
      0
      • W Wendelius

        Not sure how you want to use it but if you want to use the IsDefault in a LINQ query, consider something like this:

        /// /// One class
        ///
        public class A {
        public int a1 { get; set; }
        public bool IsDefault { get; set; }
        }

        /// /// Another class
        ///
        public class B {
        public int b1 { get; set; }
        public bool IsDefault { get; set; }
        }

        /// /// Helper to investigate value of one item
        ///
        public static class OHelper {

          public static bool GetDefaultValue(this object obj) {
             bool retValue = false;
        
             //check that there is a default field
             System.Reflection.PropertyInfo oProp = obj.GetType().GetProperty("IsDefault", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase);
        
             if (oProp != null) {
                if ((bool)oProp.GetValue(obj) == true) {
                   retValue = true;
                }
             }
             return retValue;
          }
        

        }

        And to use

             System.Collections.Generic.List<A> AList = new System.Collections.Generic.List<A>();
             System.Collections.Generic.List<B> BList = new System.Collections.Generic.List<B>();
        
             AList.Add( new A() { a1 = 1, IsDefault = true});
             AList.Add( new A() { a1 = 2, IsDefault = false});
        
             BList.Add( new B() { b1 = 1, IsDefault = false});
             BList.Add( new B() { b1 = 2, IsDefault = true});
        
             var query1 = from item in AList
                          where item.GetDefaultValue() == true
                          select item;
        
             foreach (A aItem in query1) { }
        
             var query2 = from item in BList
                          where item.GetDefaultValue() == true
                          select item;
        
             foreach (B bItem in query2) { }
        

        Of course this could be simpler if both of the classes have for example common interface which would define the existence of IsDefault property.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Excellent, I needed to change the way I was thinking and that did it. I was looking at getting the record from the collection whereas your idea uses linq and then testing each record within the linq. At the moment it is an extension method that will fall over if there is no default record flagged but I will work around it. Your idea now allows this!

        SelectedIssue.IssuerID = IssuerList.Where(x => x.IsDefaultItem() == true).First().IssuerID;

        And after a bit of fiddling I have this rather elegant solution.

        SelectedIssue.IssuerID = IssuerList.Where(x => x.IsDefaultItem() == true).DefaultIfEmpty(IssuerList.First()).FirstOrDefault().IssuerID;

        Never underestimate the power of human stupidity RAH

        W 1 Reply Last reply
        0
        • M Mycroft Holmes

          Excellent, I needed to change the way I was thinking and that did it. I was looking at getting the record from the collection whereas your idea uses linq and then testing each record within the linq. At the moment it is an extension method that will fall over if there is no default record flagged but I will work around it. Your idea now allows this!

          SelectedIssue.IssuerID = IssuerList.Where(x => x.IsDefaultItem() == true).First().IssuerID;

          And after a bit of fiddling I have this rather elegant solution.

          SelectedIssue.IssuerID = IssuerList.Where(x => x.IsDefaultItem() == true).DefaultIfEmpty(IssuerList.First()).FirstOrDefault().IssuerID;

          Never underestimate the power of human stupidity RAH

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Glad it helped :)

          1 Reply Last reply
          0
          • M Mycroft Holmes

            Some of my master tables have a boolean field IsDefault. I want to write a generic method that will accept a collection and return the item where IsDefault is true. This obviously does not work so I'm looking for ideas...

            public static object GetDefault(IEnumerable lCollection)
            {
            object oValue = null;
            if (lCollection != null)
            {
            //check that there is a default field
            PropertyInfo oProp = lCollection.First().GetType().GetProperty("IsDefault", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

            	if (oProp == null)
            	{
            		//get the item where IsDefault is true
            		oValue = lCollection.Where(x => x.IsDefault == true).FirstOrDefault();
            	}
            }
            return oValue;
            

            }

            Never underestimate the power of human stupidity RAH

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Any reason you can't use an interface?

            public interface ICanBeDefault
            {
            bool IsDefault { get; }
            }

            public class YourPocoEntityClass : ICanBeDefault
            {
            ...
            public virtual bool IsDefault { get; set; }
            ...
            }

            public static T GetDefault<T>(IEnumerable<T> collection) where T : ICanBeDefault
            {
            if (collection == null) return default(T);
            return collection.FirstOrDefault(x => x.IsDefault);
            }

            public static T GetDefault<T>(IQueryable<T> collection) where T : ICanBeDefault
            {
            if (collection == null) return default(T);
            return collection.FirstOrDefault(x => x.IsDefault);
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            M 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Any reason you can't use an interface?

              public interface ICanBeDefault
              {
              bool IsDefault { get; }
              }

              public class YourPocoEntityClass : ICanBeDefault
              {
              ...
              public virtual bool IsDefault { get; set; }
              ...
              }

              public static T GetDefault<T>(IEnumerable<T> collection) where T : ICanBeDefault
              {
              if (collection == null) return default(T);
              return collection.FirstOrDefault(x => x.IsDefault);
              }

              public static T GetDefault<T>(IQueryable<T> collection) where T : ICanBeDefault
              {
              if (collection == null) return default(T);
              return collection.FirstOrDefault(x => x.IsDefault);
              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Richard Deeming wrote:

              Any reason you can't use an interface?

              Only that the classes are generated by a code generator and putting the method in the base class work eliminates the risk of regenerating and losing the interface reference. Support will be simpler, possibly!

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups