I have the following Method (I exemplify what I need in the comments of the method):
public static Dictionary<int, int> Foo(bool os, bool rad, bool aci, bool outr, string distrito = null)
{
if (os == false && rad == false && aci == false && outr == false)
{
return new Dictionary<int, int>();
}
var parameters = MethodBase.GetCurrentMethod().GetParameters();
foreach (ParameterInfo parameter in parameters)
{
// I would love if parameter.Value existed, because:
// if (parameter.Value==true) {
// x++
// if (x == 1) string s = "true" + parameter.Name;
// if (x > 1) s += "Additional true" + parameter.Name;
// }
// s += "End";
}
return null;
}
I have to know if one or more values of the bool parameters are true. Since they are four, imagine the combination of if I would have to do to check if only one is true, or if more than one, which are true.
So, how can I cycle the current value of the incoming Method parameters without using the parameter variable itself?
If you only want to know how many are true, you can turn them into integers and add their values:
var values = new[] {os, rad, aci, outr};
var sum = values.Select(v => Convert.ToInt32(v)).Sum();
If you need the name of the parameters, then you can create an anonymous object and read its properties:
public static Dictionary<int, int> Foo(bool os, bool rad, bool aci, bool outr, string distrito = null)
{
var obj = new { os, rad, aci, outr};
foreach (PropertyInfo pInfo in obj.GetType().GetProperties())
{
var value = (bool)pInfo.GetValue(obj);
if (value)
{
//Do whatever you want here.
Console.WriteLine("{0}: {1}", pInfo.Name, value);
}
}
}
You can try some LINQ extensions, I think composition of Where and Select may be a solution, with a string.Join method on top:
// get all parameters
var parameters = MethodBase.GetCurrentMethod().GetParameters();
// for each true parameter we add this prefix
var truePrefix = "true";
// we put this string between true parameters, if more than one are present
var separator = "Additional";
// Join IEnumerable of strings with a given separator
var total = string.Join(separator, parameters
// filter parameters to check only boolean ones, and only true ones
.Where(p => p.ParameterType == typeof(bool) && (bool)p.Value)
// select a name with prefix
.Select(p => truePrefix + p.Name)))
Related
I have this Method
public static IEnumerable<T> Filter<T>(IEnumerable<T> source, string searchStr)
{
var propsToCheck = typeof(T).GetProperties().Where(a => a.PropertyType == typeof(string));
var filter = propsToCheck.Aggregate(string.Empty, (s, p) => (s == string.Empty ? string.Empty : string.Format("{0} OR ", s)) + string.Format("{0} == #0", p.Name).ToLower());
var filtered = source.AsQueryable().Where(filter, searchStr);
return filtered;
}
Which takes List and a search string and finds any matches in the list. However this only works for 100% matches, how can I make this case insensitive and use contains instead of a 100% match ?
Constructing a dynamic LINQ query doesn't look like the best option here. Filtering with a delegate would do better:
public static IEnumerable<T> Filter<T>(IEnumerable<T> source, string searchStr)
{
var searchStrLower = searchStr.ToLower();
var propsToCheck = typeof(T).GetProperties().Where(a => a.PropertyType == typeof(string) && a.CanRead);
return source.Where(obj => {
foreach (PropertyInfo prop in propsToCheck)
{
string value = (string)prop.GetValue(obj);
if (value != null && value.ToLower().Contains(searchStrLower)) return true;
}
return false;
});
}
Is their anyway of changing the below to included all fields names only and values one thing i noticted that when testing this it also brought other information about the entitiy back im only wanting the fields that have been entered or changed by the user??
public static string ObjectToNotes(object obj)
{
if (obj == null)
{
throw new ArgumentNullException("obj", "Value can not be null or Nothing!");
}
StringBuilder sb = new StringBuilder();
Type t = obj.GetType();
PropertyInfo[] pi = t.GetProperties();
for (int index = 0; index < pi.Length; index++)
{
sb.Append(pi[index].GetValue(obj, null));
if (index < pi.Length - 1)
{
sb.Append(Environment.NewLine);
}
}
return sb.ToString();
}
Right now this will save out the values for the entity only passed
As you can see from the image above the code is getting the values ok and fields but just not any drop downs related text
Help
Need more help with this how do i get the value of reference lookup values using the above method its only priting out the entity reference not the actual text value can this be done
Assuming by entered by the user you mean not having a string representation that is null or empty then try the following:
var properties = t.GetProperties();
var values = properties.Select(p => p.GetValue(obj, null)).Where(v => v != null && !string.IsNullOrEmpty(p.ToString());
var result = string.Join(Environment.NewLine, values);
In order to determine whcih fields have changed, you will need to pass two objects. One representing the entity in its pre-changed state and the other in its post-changed state and compare the properties:
var properties = t.GetProperties();
var before = properties.Select(p => new { property = p, value = p.GetValue(prechange, null) }).Where(v => v.value != null && !string.IsNullOrEmpty(p.value.ToString()).ToDictionary(p => p.property.Name, p => p.value);
var after = properties.Select(p => new { property = p, value = p.GetValue(postchange, null) }).Where(v => v.value != null && !string.IsNullOrEmpty(p.value.ToString()).ToDictionary(p => p.property.Name, p => p.value);
// You can then compare the keys / values of before and after dictionaries here
I have a table in an MSSQL database with ~300 columns and I want to extract only one row and get all the values of the columns. I've used this code but I have problems with GetValue(,) method. This is my code:
private PropertyInfo[] GetValuesDB()
{
......
var result = from val in datacTx.TableA
where val.A == "AA" + "-" + "11" &&
val.B == "CC
select val;
return result.First().GetType().GetProperties();
}
...
public void MethodA()
{
var res = GetValuesDB();
foreach (var propertyInfo in res)
{
var rez = propertyInfo.GetValue(res,null);
}
}
I always obtain this kind of exception:
Object does not match target type.
GetValue expects the type of the first argument to be the type declaring the property (or a subtype).
So your result.First() call returns a reference to an object - and you want to get the properties on that object... so that should be the reference you pass to GetValue. You'll need to change your code to return that reference:
// Ideally, change this to a more appropriate return type...
private object GetValuesDB()
{
......
var result = from val in datacTx.TableA
where val.A == "AA" + "-" + "11" &&
val.B == "CC"
select val;
return result.First();
}
...
public void MethodA()
{
var res = GetValuesDB();
foreach (var propertyInfo in res.GetType().GetProperties())
{
var rez = propertyInfo.GetValue(res, null);
}
}
So GetValuesDB now returns a reference to the relevant entity - and then you fetch the property descriptors, and ask each descriptor for the value of that property on that object.
I have collection of items which are having one enum property list.
Original property looks like
public class Content {
List<State> States {get; set;}
}
where 'State' is enum with almost 15 options.
While iterating collection of Content objects, I want to check it States property has certain values like State.Important and State.Updated exists in States and set another string from it.
something like
if(item.States.Has(State.Important) && item.States.Has(State.Updated))
string toProcess = "Do";
How to do this using Linq or Lambda ?
This should work if you must use Linq:
if (item.States.Any(state => state == State.Important) && item.States.Any(state => state == State.Updated))
Otherwise just use Contains() like #ElRonnoco says.
(However if your states are flags (powers of 2), then this answer will be slightly different.)
The trouble with this approach is that it iterates over the collection fully twice if neither of the states are set. If this happens often, it will be slower than it could be.
You can solve it without linq in a single pass like so:
bool isUpdated = false;
bool isImportant = false;
foreach (var state in item.States)
{
if (state == State.Important)
isImportant = true;
else if (state == State.Updated)
isUpdated = true;
if (isImportant && isUpdated)
break;
}
if (isImportant && isUpdated)
{
// ...
}
This is unlikely to be an issue unless you have very large lists which often don't have either of the target states set, so you're probably best off using El Ronnoco's solution anyway.
If you have a lot of states to deal with, you could simplify things by writing an extension method like so:
public static class EnumerableExt
{
public static bool AllPredicatesTrueOverall<T>(this IEnumerable<T> self, params Predicate<T>[] predicates)
{
bool[] results = new bool[predicates.Length];
foreach (var item in self)
{
for (int i = 0; i < predicates.Length; ++i)
if (predicates[i](item))
results[i] = true;
if (results.All(state => state))
return true;
}
return false;
}
I had some difficulty coming up for a name for this. It will return true if for each predicate there is at least one item in the sequence for which the predicate is true. But that's a bit long for a method name... ;)
Then your example would become:
if (item.States.AllPredicatesTrueOverall(s => s == State.Important, s => s == State.Updated))
Here's some sample code that uses it:
enum State
{
Unknown,
Important,
Updated,
Deleted,
Other
}
void run()
{
IEnumerable<State> test1 = new[]
{
State.Important,
State.Updated,
State.Other,
State.Unknown
};
if (test1.AllPredicatesTrueOverall(s => s == State.Important, s => s == State.Updated))
Console.WriteLine("test1 passes.");
else
Console.WriteLine("test1 fails.");
IEnumerable<State> test2 = new[]
{
State.Important,
State.Other,
State.Other,
State.Unknown
};
if (test2.AllPredicatesTrueOverall(s => s == State.Important, s => s == State.Updated))
Console.WriteLine("test2 passes.");
else
Console.WriteLine("test2 fails.");
// And to show how you can use any number of predicates:
bool result = test1.AllPredicatesTrueOverall
(
state => state == State.Important,
state => state == State.Updated,
state => state == State.Other,
state => state == State.Deleted
);
}
But perhaps the easiest is to write an extension method for IEnumerable<State> (if you only have the one state enum to worry about):
public static class EnumerableStateExt
{
public static bool AllStatesSet(this IEnumerable<State> self, params State[] states)
{
bool[] results = new bool[states.Length];
foreach (var item in self)
{
for (int i = 0; i < states.Length; ++i)
if (item == states[i])
results[i] = true;
if (results.All(state => state))
return true;
}
return false;
}
}
Then your original code will become:
if (item.States.AllStatesSet(State.Important, State.Updated))
and you can easily specify more states:
if (item.States.AllStatesSet(State.Important, State.Updated, State.Deleted))
You don't need Linq. I don't thinq
if(item.States.Contains(State.Important) && item.States.Contains(State.Updated))
string toProcess = "Do";
http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx
List has a Contains method, so your code would be
if(item.States.Contains(State.Important) && item.States.Contains(State.Updated))
string toProcess = "Do";
I see no real benefit in using Linq or a lambda expression here...
You could go with
!(new List<States>{State.Important, State.Updated}.Except(item.States).Any());
It's not really shorter, but easier if you have a huge number of states to check.
As long as you want to check that the item has all states needed, you just have to add new States to the first list.
var res = (from items in item
where items.States.Has(State.Important) && items.States.Has(State.Updated)
select new { NewProcess = "Do" }).ToList();
foreach (var result in res)
{
string result = result.NewProcess
}
Try this
Maybe you could consider using your enum as a set of flags, i.e. you can combine multiple states without having a list:
[Flags]
public enum State
{
Important = 1,
Updated = 2,
Deleted = 4,
XXX = 8
....
}
public class Content
{
public State MyState { get; set; }
}
if ((myContent.MyState & State.Important) == State.Important
&& (myContent.MyState & State.Updated) == State.Updated)
{
// Important AND updated
}
Some sort of following implementation
Content obj = new Content();
obj.States = SomeMethod();
if(obj.States.Any(h => h == State.Important) && obj.States.Any(h => h == State.Updated))
{
string toProcess = "Do";
}
I have a method that gets a list of objects and then returns all their properties values based on the attributes and types of the property.
I'm having difficulties with selecting the value because the Select depends on conditions.
What I did so far allows me to get the value in the ugliest way I have ever seen:
public IEnumerable<string> GetValues(List<ProperyInfo> objects)
{
var allValues = new List<string>();
foreach (var obj in objects)
{
// Get the PropertyInfo for the obj
var properties = GetPropertiesWithTheAttributes(obj);
var values = properties.Select(x => new
{
Value = x.GetValue(obj, null) == null
? string.empty
:
x.PropertyType.BaseType == typeof(Enum)
? Convert.ToInt32(x.GetValue(obj, null)).ToString()
: (x.GetValue(obj, null)).ToString(),
((ReportAttribute)
Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order
})
.OrderBy(x => x.Order)
.Select(x => x.Value.ToString())
.ToList();
allValues.AddRange(values);
}
return allValues;
}
And in the code I published here I even removed the check for the DisplayDate property in the ReportAttribute, which verifies if the datetime property of the attribute would be displayed as date or datetime...
serenity now!
I would simply extract this into 2 methods, and get rid of the ternary operator:
// not sure how to name 'obj' and 'x' without more context
private static object GetValue(PropertyInfo obj, PropertyInfo x)
{
if (x.GetValue(obj, null) == null) return string.Empty;
if (x.PropertyType.BaseType == typeof (Enum))
return Convert.ToInt32(x.GetValue(obj, null));
return x.GetValue(obj, null);
}
private static int GetOrder(PropertyInfo x)
{
return ((ReportAttribute) Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order;
}
So you can write:
public IEnumerable<string> GetValues(List<PropertyInfo> objects)
{
var allValues = new List<string>();
foreach (var obj in objects)
{
// Get the PropertyInfo for the obj
var properties = GetPropertiesWithTheAttributes(obj);
var values = properties.Select(x => new
{
Value = GetValue(obj, x),
Order = GetOrder(x)
})
.OrderBy(x => x.Order)
.Select(x => x.Value.ToString())
.ToList();
allValues.AddRange(values);
}
return allValues;
}
If you really want to inline this, you can change the lambda expression in your Select to a statement:
.Select(x =>
{
object value;
if (x.GetValue(obj, null) == null) value = string.Empty;
else if (x.PropertyType.BaseType == typeof (Enum))
value = Convert.ToInt32(x.GetValue(obj, null));
else value = x.GetValue(obj, null);
return new
{
Value = value,
((ReportAttribute)
Attribute.GetCustomAttribute(x, typeof (ReportAttribute), false)).
Order
};
})