Query any collection
-
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
-
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
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.
-
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.
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
-
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
-
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
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
-
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
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