I want to use more elegant construction - c#

I am not senior at C#, so probably I should learn more basic, but here is my code:
var out1 = obj1.F1();
var out2 = obj2.F2(out1);
var out3 = obj3.F3(out2);
var out4 = obj4.F4(out3);
Other way is to do the same in one line:
var out4 = obj4.F4( obj3.F3( obj2.F2( obj1.F1() ) ) );
But it's difficult to read, more hierarchically, reminds xml tags
So I want to make it more functional. It should look like 'pipeline', 'conveyor' where out of one function is input of other. My solution (with crutches):
var obj = new[]{0}; //first looking bad thing, without that you are operating with each element of F1() result
var something = obj
.Select(x => obj1.F1() ) //second: x=> x doesn't take part in right side
.Select(x => obj2.F2(x) )
.Select(x => obj3.F3(x) )
.Select(x => obj4.F4(x) )
.ToArray(); //third: without that it doesn't do anything
How should you code this 'pattern'?

Rather than trying to use Enumerable.Select, which is designed for sequences (which is not what you have) you can simply create an operator that performs the same operation on individual items:
public static TResult Map<TSource, TResult>(
this TSource source, Func<TSource, TResult> selector)
{
return selector(source);
}
You can then use that without the awkward transformations to/from a sequence:
var result = obj1.F1()
.Map(x => obj2.F2(x))
.Map(x => obj3.F3(x))
.Map(x => obj4.F4(x));

You could do something like this:
var result = obj1.F1().Pipe(obj2.F2).Pipe(obj3.F3).Pipe(obj4.F4);
(using the following extension method)
static class Extensions
{
public static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> func)
{
return func(input);
}
}
Not sure it's really more readable, though...

Well, your example is a little poor because I can't tell what exactly these methods are doing. They aren't named anything that infers their purpose, so we can't really give you much advice on how to proceed.
For instance, lets say that this was an image processing library and you were doing something like:
(this is psuedocode)
var img = new Image();
var trimmedImage = TrimWhitespace(img);
var rotatedImage = RotateImage(trimmedImage);
var zoomedImage = ZoomImage(rotatedImage);
I would still never chain them, but if I needed to reuse it in a presentation layer format, I'd setup an object that orchestrates it, ie:
var formattedImage = ImageFormatter.Format(new Image());
and ImageFormatter would call them in the order outlined in the psuedocode.

Related

C#: Make System.Data.Services.Client.DataServiceQuery async by default

Mono.Android requires me to do async queries. I can do that like this:
DataServiceQuery<T> dataServiceQuery = query as DataServiceQuery<T>;
return (new TaskFactory<IEnumerable<T>>()).FromAsync(dataServiceQuery.BeginExecute(null, null), asyncResult => dataServiceQuery.EndExecute(asyncResult)).Result;
The problem is that I don't want to call this method every time I'm querying because this forces my users that are using my library to call that method, otherwise Mono will crash, so I'd like to make DataServiceQuery do this by default. I can do this by overriding the GetEnumerator() function from DataServiceQuery, but it's constructor is private so I'm not allowed to do that. The only option I have left I guess is to recompile System.Data.Services.Client.dll with my changes. Maybe there is a trick with DataServiceContext but I'm honestly out of options.
Thanks for your help,
Maxim
It is not possible to do this automatically, but you can make your statement nicer by making a new Select function called SelectAsync.
As a matter of fact this also fixes a problem with OData and LINQ because you had to wrap Selects in anonymous types, now you can call the Select after the query (which takes longer since you are querying the whole class, but this is not a problem to me).
Here is the code for the SelectAsync extension methods:
public static IEnumerable<T> SelectAsync<T>(this IQueryable<T> queryable)
{
return queryable.SelectAsync(x => x);
}
public static IEnumerable<TResult> SelectAsync<T, TResult>(this IQueryable<T> queryable, Function<T, TResult> selector)
{
var dataServiceQuery = queryable as DataServiceQuery<T>;
return Task.Run(async () => (await (new TaskFactory<IEnumerable<T>>()).FromAsync(dataServiceQuery.BeginExecute, dataServiceQuery. EndExecute, null)).Select(selector)).Result;
}
Executing a query with LINQ in Mono.Android is now more intuitive.
var itemNames = Items.Where(item => item.ID == 0).Select(item => item.Name);
var goodCars = Cars.Where(car => car.Fuel >= 0.5);
Translates to:
var itemNames = Items.Where(item => item.ID == 0).SelectAsync(item => item.Name);
var goodCars = Cars.Where(car => car.Fuel >= 0.5).SelectAsync();
As far as I know, this is as good as it can get because System.Data.Services is currently not fully supported in Mono.Android. This is a nice and intuitive workaround.
Sources:
https://www.mono-project.com/docs/web/wcf/

Get correct method through reflection when parameters only differ in generic type [duplicate]

I want to select the right generic method via reflection and then call it.
Usually this is quite easy. For example
var method = typeof(MyType).GetMethod("TheMethod");
var typedMethod = method.MakeGenericMethod(theTypeToInstantiate);
However the issue start when there are different generic overloads of the method. For example the static-methods in the System.Linq.Queryable-class. There are two definitions of the 'Where'-method
static IQueryable<T> Where(this IQueryable<T> source, Expression<Func<T,bool>> predicate)
static IQueryable<T> Where(this IQueryable<T> source, Expression<Func<T,int,bool>> predicate)
This meand that GetMethod doesn't work, because it cannot destiguish the two. Therefore I want to select the right one.
So far I often just took the first or second method, depending on my need. Like this:
var method = typeof (Queryable).GetMethods().First(m => m.Name == "Where");
var typedMethod = method.MakeGenericMethod(theTypeToInstantiate);
However I'm not happy with this, because I make a huge assumption that the first method is the right one. I rather want to find the right method by the argument type. But I couldn't figure out how.
I tried it with passing the 'types', but it didn't work.
var method = typeof (Queryable).GetMethod(
"Where", BindingFlags.Static,
null,
new Type[] {typeof (IQueryable<T>), typeof (Expression<Func<T, bool>>)},
null);
So has anyone an idea how I can find the 'right' generic method via reflection. For example the right version of the 'Where'-method on the Queryable-class?
You can somewhat elegantly select a specific generic overload of a method at compile-time, without passing any strings to run-time searches like the other answers here do.
Static Methods
Suppose you have multiple static methods of the same name like:
public static void DoSomething<TModel>(TModel model)
public static void DoSomething<TViewModel, TModel>(TViewModel viewModel, TModel model)
// etc
If you create an Action or Func that matches the generic count and parameter count of the overload you're looking for, you can select it at compile-time with relatively few acrobatics.
Example: Select the first method - returns void, so use an Action, takes one generic. We use object to avoid specifying type just yet:
var method = new Action<object>(MyClass.DoSomething<object>);
Example: Select the second method - returns void, so Action, 2 generic types so use type object twice, once for each of the 2 generic parameters:
var method = new Action<object, object>(MyClass.DoSomething<object, object>);
You just got the method you wanted without doing any crazy plumbing, and no run-time searching or usage of risky strings.
MethodInfo
Typically in Reflection you want the MethodInfo object, which you can also get in a compile-safe way. This is when you pass the actual generic types you want to use in your method. Assuming you wanted the second method above:
var methodInfo = method.Method.MakeGenericMethod(type1, type2);
There's your generic method without any of the reflection searching or calls to GetMethod(), or flimsy strings.
Static Extension Methods
The specific example you cite with Queryable.Where overloads forces you to get a little fancy in the Func definition, but generally follows the same pattern. The signature for the most commonly used Where() extension method is:
public static IQueryable<TModel> Where<TModel>(this IQueryable<TModel>, Expression<Func<TModel, bool>>)
Obviously this will be slightly more complicated - here it is:
var method = new Func<IQueryable<object>,
Expression<Func<object, bool>>,
IQueryable<object>>(Queryable.Where<object>);
var methodInfo = method.Method.MakeGenericMethod(modelType);
Instance Methods
Incorporating Valerie's comment - to get an instance method, you'll need to do something very similar. Suppose you had this instance method in your class:
public void MyMethod<T1>(T1 thing)
First select the method the same way as for statics:
var method = new Action<object>(MyMethod<object>);
Then call GetGenericMethodDefinition() to get to the generic MethodInfo, and finally pass your type(s) with MakeGenericMethod():
var methodInfo = method.Method.GetGenericMethodDefinition().MakeGenericMethod(type1);
Decoupling MethodInfo and Parameter Types
This wasn't requested in the question, but once you do the above you may find yourself selecting the method in one place, and deciding what types to pass it in another. You can decouple those 2 steps.
If you're uncertain of the generic type parameters you're going to pass in, you can always acquire the MethodInfo object without them.
Static:
var methodInfo = method.Method;
Instance:
var methodInfo = method.Method.GetGenericMethodDefinition();
And pass that on to some other method that knows the types it wants to instantiate and call the method with - for example:
processCollection(methodInfo, type2);
...
protected void processCollection(MethodInfo method, Type type2)
{
var type1 = typeof(MyDataClass);
object output = method.MakeGenericMethod(type1, type2).Invoke(null, new object[] { collection });
}
One thing this especially helps with is selecting a specific instance method of a class, from inside the class, then exposing that to outside callers who need it with various types later on.
Addendum
A number of comments below say they cannot get this to work. It might not be surprising that I don't often have to select a generic method like this, but I happen to be doing so today, in well-tested code used behind the scenes all the time, so I thought I'd provide that real-world example - and perhaps it will help those who struggle to get this to work.
C# lacks a Clone method, so we have our own. It can take a number of arguments, including those that explain how to recursively copy IEnumerable properties inside the source object.
The method that copies an IEnumerable is named CopyList, and looks like this:
public static IEnumerable<TTo> CopyList<TTo>(
IEnumerable<object> from,
Func<PropertyInfo, bool> whereProps,
Dictionary<Type, Type> typeMap
)
where TTo : new()
{
To complicate things (and flex the muscles of this approach), it has several overloads, like this one:
public static IEnumerable<TTo> CopyList<TTo>(
IEnumerable<object> from,
Dictionary<Type, Type> typeMap
)
where TTo : new()
{
So, we've got several (I'm only showing you 2, but there are more in the code) method signatures. They have the same number of Generic arguments, but a different number of method arguments. The names are identical. How are we possibly going to call the right method? Begin the C# ninjaing!
var listTo = ReflectionHelper.GetIEnumerableType(
fromValue.GetType());
var fn = new Func<
IEnumerable<object>,
Func<PropertyInfo, bool>,
Dictionary<Type, Type>,
IEnumerable<object>>(
ModelTransform.CopyList<object>);
var copyListMethod = fn.GetMethodInfo()
.GetGenericMethodDefinition()
.MakeGenericMethod(listTo);
copyListMethod.Invoke(null,
new object[] { fromValue, whereProps, typeMap });
The first line uses a helper method we'll come back to, but all it's doing is getting the generic type of the IEnumerable list in this property, and assigning it to listTo. The next line is where we really begin performing this trick, where we lay out a Func with adequate parameters to match up with the specific CopyList() overload we intend to grab. Specifically, the CopyList() we want has 3 arguments, and returns IEnumerable<TTo>. Remember that Func takes its return type as its last generic arg, and that we're substituting object wherever there's a generic in the method we intend to grab. But, as you can see in this example, we do not need to substitute object anywhere else. For example, we know we want to pass a where clause that accepts a PropertyInfo and returns true/false (bool), and we just say those types right in the Func.
As the constructor arg to the Func, we pass CopyList() - but remember that the name CopyList is vague because of the method overloads. What's really cool is that C# is doing the hard work for you right now, by looking at the Func args, and identifying the right one. In fact, if you get the types or number of args wrong, Visual Studio will actually mark the line with an error:
No overload for 'CopyList' matches delegate 'Func...'
It's not smart enough to tell you what exactly you need to fix, but if you see that error you're close - you need to carefully double-check the args and return type and match them up exactly, replacing Generic args with object.
On the third line, we call the C# built-in .GetMethodInfo() and then .MakeGeneric(listTo). We have only one Generic to set for this, so we pass that in as listTo. If we had 2, we'd pass 2 args here. These Type args are replacing the object substitutions we made earlier.
And that's it - we can call copyListMethod(), with no strings, fully compile-safe. The final line makes the call, first passing null because it's a static method, then an object[] array with the 3 args. Done.
I said I'd come back to the ReflectionHelper method. Here it is:
public static Type GetIEnumerableType(Type type)
{
var ienumerable = type.GetInterface(typeof(System.Collections.Generic.IEnumerable<>).FullName);
var generics = ienumerable.GetGenericArguments();
return generics[0];
}
It can be done, but it's not pretty!
For example, to get the first overload of Where mentioned in your question you could do this:
var where1 = typeof(Queryable).GetMethods()
.Where(x => x.Name == "Where")
.Select(x => new { M = x, P = x.GetParameters() })
.Where(x => x.P.Length == 2
&& x.P[0].ParameterType.IsGenericType
&& x.P[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>)
&& x.P[1].ParameterType.IsGenericType
&& x.P[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>))
.Select(x => new { x.M, A = x.P[1].ParameterType.GetGenericArguments() })
.Where(x => x.A[0].IsGenericType
&& x.A[0].GetGenericTypeDefinition() == typeof(Func<,>))
.Select(x => new { x.M, A = x.A[0].GetGenericArguments() })
.Where(x => x.A[0].IsGenericParameter
&& x.A[1] == typeof(bool))
.Select(x => x.M)
.SingleOrDefault();
Or if you wanted the second overload:
var where2 = typeof(Queryable).GetMethods()
.Where(x => x.Name == "Where")
.Select(x => new { M = x, P = x.GetParameters() })
.Where(x => x.P.Length == 2
&& x.P[0].ParameterType.IsGenericType
&& x.P[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>)
&& x.P[1].ParameterType.IsGenericType
&& x.P[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>))
.Select(x => new { x.M, A = x.P[1].ParameterType.GetGenericArguments() })
.Where(x => x.A[0].IsGenericType
&& x.A[0].GetGenericTypeDefinition() == typeof(Func<,,>))
.Select(x => new { x.M, A = x.A[0].GetGenericArguments() })
.Where(x => x.A[0].IsGenericParameter
&& x.A[1] == typeof(int)
&& x.A[2] == typeof(bool))
.Select(x => x.M)
.SingleOrDefault();
This question is about 2 years old, but I came up with (what I think is) an elegant solution, and thought I'd share it with the fine folks here at StackOverflow. Hopefully it will help those who arrive here via various search queries.
The problem, as the poster stated, is to get the correct generic method. For example, a LINQ extension method may have tons of overloads, with type arguments nested inside other generic types, all used as parameters. I wanted to do something like this:
var where = typeof(Enumerable).GetMethod(
"Where",
typeof(IQueryable<Refl.T1>),
typeof(Expression<Func<Refl.T1, bool>>
);
var group = typeof(Enumerable).GetMethod(
"GroupBy",
typeof(IQueryable<Refl.T1>),
typeof(Expression<Func<Refl.T1, Refl.T2>>
);
As you can see, I've created some stub types "T1" and "T2", nested classes within a class "Refl" (a static class which contains all my various Reflection utility extension functions, etc. They serve as placeholders for where the type parameters would have normally went. The examples above correspond to getting the following LINQ methods, respectively:
Enumerable.Where(IQueryable<TSource> source, Func<TSource, bool> predicate);
Enumerable.GroupBy(IQueryable<Source> source, Func<TSource, TKey> selector);
So it should be clear that Refl.T1 goes where TSource would gone, in both of those calls; and the Refl.T2 represents the TKey parameter.The TX classes are declared as such:
static class Refl {
public sealed class T1 { }
public sealed class T2 { }
public sealed class T3 { }
// ... more, if you so desire.
}
With three TX classes, your code can identify methods containing up to three generic type parameters.
The next bit of magic is to implement the function that does the search via GetMethods():
public static MethodInfo GetMethod(this Type t, string name, params Type[] parameters)
{
foreach (var method in t.GetMethods())
{
// easiest case: the name doesn't match!
if (method.Name != name)
continue;
// set a flag here, which will eventually be false if the method isn't a match.
var correct = true;
if (method.IsGenericMethodDefinition)
{
// map the "private" Type objects which are the type parameters to
// my public "Tx" classes...
var d = new Dictionary<Type, Type>();
var args = method.GetGenericArguments();
if (args.Length >= 1)
d[typeof(T1)] = args[0];
if (args.Length >= 2)
d[typeof(T2)] = args[1];
if (args.Length >= 3)
d[typeof (T3)] = args[2];
if (args.Length > 3)
throw new NotSupportedException("Too many type parameters.");
var p = method.GetParameters();
for (var i = 0; i < p.Length; i++)
{
// Find the Refl.TX classes and replace them with the
// actual type parameters.
var pt = Substitute(parameters[i], d);
// Then it's a simple equality check on two Type instances.
if (pt != p[i].ParameterType)
{
correct = false;
break;
}
}
if (correct)
return method;
}
else
{
var p = method.GetParameters();
for (var i = 0; i < p.Length; i++)
{
var pt = parameters[i];
if (pt != p[i].ParameterType)
{
correct = false;
break;
}
}
if (correct)
return method;
}
}
return null;
}
The code above does the bulk of the work -- it iterates through all the Methods in a particular type, and compares them to the given parameter types to search for. But wait! What about that "substitute" function? That's a nice little recursive function that will search through the entire parameter type tree -- after all, a parameter type can itself be a generic type, which may contain Refl.TX types, which have to be swapped for the "real" type parameters which are hidden from us.
private static Type Substitute(Type t, IDictionary<Type, Type> env )
{
// We only really do something if the type
// passed in is a (constructed) generic type.
if (t.IsGenericType)
{
var targs = t.GetGenericArguments();
for(int i = 0; i < targs.Length; i++)
targs[i] = Substitute(targs[i], env); // recursive call
t = t.GetGenericTypeDefinition();
t = t.MakeGenericType(targs);
}
// see if the type is in the environment and sub if it is.
return env.ContainsKey(t) ? env[t] : t;
}
Another solution that you might find useful - it is possible to get a MethodInfo based on Expression.Call that already has a logic for overload resolution.
For example, in case you need to get some specific Enumerable.Where method that could be accomplished using the following code:
var mi = Expression.Call(typeof (Enumerable), "Where", new Type[] {typeof (int)},
Expression.Default(typeof (IEnumerable<int>)), Expression.Default(typeof (Func<int, int, bool>))).Method;
Third argument in the example - describes types of generic arguments, and all other arguments - types of parameters.
In the same way it is possible to get even non static object generic methods.You need to change only first argument from typeof (YourClass) to Expression.Default(typeof (YourClass)).
Actually, I have used that approach in my plugin for .NET Reflection API. You may check how it works here
Chris Moschini's answer is good when you know the method name in compile time. Antamir's answer works if we get method name in runtime, but is quite an overkill.
I am using another way, for which I got inspiration using reflector from .NET function Expression.Call, which selects correct generic method from a string.
public static MethodInfo GetGenericMethod(Type declaringType, string methodName, Type[] typeArgs, params Type[] argTypes) {
foreach (var m in from m in declaringType.GetMethods()
where m.Name == methodName
&& typeArgs.Length == m.GetGenericArguments().Length
&& argTypes.Length == m.GetParameters().Length
select m.MakeGenericMethod(typeArgs)) {
if (m.GetParameters().Select((p, i) => p.ParameterType == argTypes[i]).All(x => x == true))
return m;
}
return null;
}
Usage:
var m = ReflectionUtils.GetGenericMethod(typeof(Queryable), "Where", new[] { typeof(Person) }, typeof(IQueryable<Person>), typeof(Expression<Func<Person, bool>>));
If you need only generic method definition or simply do not know the type T at the time, you can use some bogus types and then strip the generic's information:
var m = ReflectionUtils.GetGenericMethod(typeof(Queryable), "Where", new[] { typeof(object) }, typeof(IQueryable<object>), typeof(Expression<Func<object, bool>>));
m = m.GetGenericMethodDefinition();
Let the compiler do it for you:
var fakeExp = (Expression<Func<IQueryable<int>, IQueryable<int>>>)(q => q.Where((x, idx) => x> 2));
var mi = ((MethodCallExpression)fakeExp.Body).Method.GetGenericMethodDefinition();
for the Where with index, or simply leave out the second parameter in the Where expression for the one without
In additional to #MBoros's answer.
You can avoid writing complex generic arguments using this helper method:
public static MethodInfo GetMethodByExpression<Tin, Tout>(Expression<Func<IQueryable<Tin>, IQueryable<Tout>>> expr)
{
return (expr.Body as MethodCallExpression).Method;
}
Usage:
var where = GetMethodByExpression<int, int>(q => q.Where((x, idx) => x > 2));
Or
var select = GetMethodByExpression<Person, string>(q => q.Select(x => x.Name));
Use DynamicMethods.GenericMethodInvokerMethod, GetMethod is not enough to use with generics
I made a little helper func:
Func<Type, string, Type[], Type[], MethodInfo> getMethod = (t, n, genargs, args) =>
{
var methods =
from m in t.GetMethods()
where m.Name == n && m.GetGenericArguments().Length == genargs.Length
let mg = m.IsGenericMethodDefinition ? m.MakeGenericMethod(genargs) : m
where mg.GetParameters().Select(p => p.ParameterType).SequenceEqual(args)
select mg
;
return methods.Single();
};
Works for simple non-generics:
var m_movenext = getMethod(typeof(IEnumerator), nameof(IEnumerator.MoveNext), Type.EmptyTypes, Type.EmptyTypes);
Like for complicated generics:
var t_source = typeof(fillin1);
var t_target = typeof(fillin2);
var m_SelectMany = getMethod(
typeof(Enumerable),
nameof(Enumerable.SelectMany),
new[] {
t_source,
t_target
},
new[] {
typeof(IEnumerable<>).MakeGenericType(t_source),
typeof(Func<,>).MakeGenericType(t_source, typeof(IEnumerable<>).MakeGenericType(t_target))
});
I have a similar issue and I thought I would post my solution here. I'm trying to call several functions:
p.Foo<Klass1>(true)
p.Foo<Klass2>(true)
p.Foo<Klass3>(true)
bool k1 = p.Bar<Klass1>()
bool k2 = p.Bar<Klass2>()
bool k3 = p.Bar<Klass3>()
My solution:
public static TAction RemapGenericMember<TAction>(object parent, Type target, TAction func) where TAction : Delegate {
var genericMethod = func?.Method?.GetGenericMethodDefinition()?.MakeGenericMethod(target);
if (genericMethod.IsNull()) {
throw new Exception($"Failed to build generic call for '{func.Method.Name}' with generic type '{target.Name}' for parent '{parent.GetType()}'");
}
return (TAction)genericMethod.CreateDelegate(typeof(TAction), parent);
}
And now I can call:
foreach(var type in supportedTypes) {
InvokeGenericMember<Action<bool>>(p, type, Foo<object>)(true);
bool x = InvokeGenericMember<Function<bool>>(p, type, Bar<object>)();
}
Antamir's answer was very useful for me, but it has a bug in that it doesn't validate that the number of parameters on the method found matches the number of types passed in when you provide a mix of generic and concrete types.
For example, if you ran:
type.GetMethod("MyMethod",typeof(Refl.T1),typeof(bool))
it can't differentiate between two methods:
MyMethod<T>(T arg1)
MyMethod<T>(T arg1, bool arg2)
The two calls to:
var p = method.GetParameters();
should be changed to:
var p = method.GetParameters();
if (p.Length != parameters.Length)
{
correct = false;
continue;
}
Also, both of the existing 'break' lines should be 'continue'.
I found out the easiest way to use iQuerable expressions while calling method using reflection. Please see below code:
You can use the IQuerable expression as per requirement.
var attributeName = "CarName";
var attributeValue = "Honda Accord";
carList.FirstOrDefault(e => e.GetType().GetProperty(attributeName).GetValue(e, null) as string== attributeValue);
var firstGenericParam = Type.MakeGenericMethodParameter(0);
var firstParam = typeof(IQueryable<>).MakeGenericType(firstGenericParam);
var funcType = typeof(Func<,>).MakeGenericType(firstGenericParam, typeof(bool));
//var funcType = typeof(Func<,,>).MakeGenericType(firstGenericParam, typeof(int), typeof(bool)); //for second version
var secondParam = typeof(Expression<>).MakeGenericType(funcType);
var method = typeof(Queryable).GetMethod(nameof(Queryable.Where), new Type[] { firstParam, secondParam });

Why doesn't IOrderedEnumerable retain order after where filtering

I've created a simplification of the issue. I have an ordered IEnumerable, I'm wondering why applying a where filter could unorder the objects
This does not compile while it should have the potential to
IOrderedEnumerable<int> tmp = new List<int>().OrderBy(x => x);
//Error Cannot Implicitly conver IEnumerable<int> To IOrderedEnumerable<int>
tmp = tmp.Where(x => x > 1);
I understand that there would be no gaurenteed execution order if coming from an IQueryable such as using linq to some DB Provider.
However, when dealing with Linq To Object what senario could occur that would unorder your objects, or why wasn't this implemented?
EDIT
I understand how to properly order this that is not the question. My Question is more of a design question. A Where filter on linq to objects should enumerate the give enumerable and apply filtering. So why is that we can only return an IEnumerable instead of an IOrderedEnumerable?
EDIT
To Clarify the senario in when this would be userful. I'm building Queries based on conditions in my code, I want to reuse as much code as possible. I have a function that is returning an OrderedEnumerable, however after applying the additional where I would have to reorder this even though it would be in its original ordered state
Rene's answer is correct, but could use some additional explanation.
IOrderedEnumerable<T> does not mean "this is a sequence that is ordered". It means "this is a sequence that has had an ordering operation applied to it and you may now follow that up with a ThenBy to impose additional ordering requirements."
The result of Where does not allow you to follow it up with ThenBy, and therefore you may not use it in a context where an IOrderedEnumerable<T> is required.
Make sense?
But of course, as others have said, you almost always want to do the filtering first and then the ordering. That way you are not spending time putting items into order that you are just going to throw away.
There are of course times when you do have to order and then filter; for example, the query "songs in the top ten that were sung by a woman" and the query "the top ten songs that were sung by a woman" are potentially very different! The first one is sort the songs -> take the top ten -> apply the filter. The second is apply the filter -> sort the songs -> take the top ten.
The signature of Where() is this:
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
So this method takes an IEnumerable<int> as first argument. The IOrderedEnumerable<int> returned from OrderBy implements IEnumerable<int> so this is no problem.
But as you can see, Where returns an IEnumerable<int> and not an IOrderedEnumerable<int>. And this cannot be casted into one another.
Anyway, the object in that sequence will still have the same order. So you could just do it like this
IEnumerable<int> tmp = new List<int>().OrderBy(x => x).Where(x => x > 1);
and get the sequence you expected.
But of course you should (for performance reasons) filter your objects first and sort them afterwards when there are fewer objects to sort:
IOrderedEnumerable<int> tmp = new List<int>().Where(x => x > 1).OrderBy(x => x);
The tmp variable's type is IOrderedEnumerable.
Where() is a function just like any other with a return type, and that return type is IEnumerable. IEnumerable and IOrderedEnumerable are not the same.
So when you do this:
tmp = tmp.Where(x => x > 1);
You are trying to assign the result of a Where() function call, which is an IEnuemrable, to the tmp variable, which is an IOrderedEnumerable. They are not directly compatible, there is no implicit cast, and so the compiler sends you an error.
The problem is you are being too specific with the tmp variable's type. You can make one simple change that will make this all work by being just be a little less specific with your tmp variable:
IEnumerable<int> tmp = new List<int>().OrderBy(x => x);
tmp = tmp.Where(x => x > 1);
Because IOrderedEnumerable inherits from IEnumerable, this code will all work. As long as you don't want to call ThenBy() later on, this should give you exactly the same results as you expect without any other loss of ability to use the tmp variable later.
If you really need an IOrderedEnumerable, you can always just call .OrderBy(x => x) again:
IOrderedEnumerable<int> tmp = new List<int>().OrderBy(x => x);
tmp = tmp.Where(x => x > 1).OrderBy(x => x);
And again, in most cases (not all, but most) you want to get your filtering out of the way before you start sorting. In other words, this is even better:
var tmp = new List<int>().Where(x => x > 1).OrderBy(x => x);
why wasn't this implemented?
Most likely because the LINQ designers decided that the effort to implement, test, document etc. isn't worth enough compared to the potential use cases. In fact your are the first one I hear complaining about that.
But if it's so important to you, you can add that missing functionality yourself (similar to #Jon Skeet MoreLINQ extension library). For instance, something like this:
namespace MyLinq
{
public static class Extensions
{
public static IOrderedEnumerable<T> Where<T>(this IOrderedEnumerable<T> source, Func<T, bool> predicate)
{
return new WhereOrderedEnumerable<T>(source, predicate);
}
class WhereOrderedEnumerable<T> : IOrderedEnumerable<T>
{
readonly IOrderedEnumerable<T> source;
readonly Func<T, bool> predicate;
public WhereOrderedEnumerable(IOrderedEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
this.source = source;
this.predicate = predicate;
}
public IOrderedEnumerable<T> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer, bool descending) =>
new WhereOrderedEnumerable<T>(source.CreateOrderedEnumerable(keySelector, comparer, descending), predicate);
public IEnumerator<T> GetEnumerator() => Enumerable.Where(source, predicate).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}
And putting it into action:
using System;
using System.Collections.Generic;
using System.Linq;
using MyLinq;
var test = Enumerable.Range(0, 100)
.Select(n => new { Foo = 1 + (n / 20), Bar = 1 + n })
.OrderByDescending(e => e.Foo)
.Where(e => (e.Bar % 2) == 0)
.ThenByDescending(e => e.Bar) // Note this compiles:)
.ToList();

Linq, using ToLookup to project values to different named variables

var tmpProjection = myCollection.ToLookup(t => t.SomeBoolValue);
var listOneFinal = tmpProjection[true];
var listTwo = tmpProjection[false];
First question, is there a way to assign it to listOne and listTwo in some shorter way, I know I'm being pedantic here, ... just asking.
Now,
var listThree = listTwo.ToLookup(t => t.SomeOtherBoolValue);
var listFourFinal = listThree[false];
var listFiveFinal = listThree[true];
So in thise case, I just need (ultimately) listOneFinal, listFourFinal and listFiveFinal -- but i'm creating this temp stuff in between ... is there a way to reduce this.
i'm only talk code-wise not performance or code criticality.
bool is kind of weak for communicating intent. Int is a little better and enum would be best.
Lookup<int, T> myLookup = myCollection
.ToLookup(t =>
t.someBoolValue ? 1 :
t.someOtherBoolValue ? 4 :
5
);
var listOne = myLookup[1];
var listFour = myLookup[4];
var listFive = myLookup[5];
You can do it in fewer statements, but since you need to end op with 3 values, you need at least 3 assignments. Your code is very readable, don't sacrifice readability for "being smart" and reducing to fewer statements. That being said, here is a 3 - statement version; that will work well if collections are small (your own version will perform better with larger collections, since this version iterates multiple times through the collection):
var listOneFinal = myCollection.Where(t => t.SomeBoolValue);
var listFourFinal = myCollection.Where(t => !t.SomeBoolValue && !t.SomeOtherBoolValue);
var listFiveFinal = myCollection.Where(t => !t.SomeBoolValue && t.SomeOtherBoolValue);
Depending on your real usage scenario, the above might be more readable.
I think what you've got there is pretty good already, to be honest.
If you're simply looking to minimize the number of statements, I doubt you could do better than:
var listOneFinal = myCollection.Where(t => t.SomeBoolValue);
var listFourFinal = myCollection.Where(t => !t.SomeBoolValue && !t.SomeOtherBoolValue);
var listFiveFinal = myCollection.Where(t => !t.SomeBoolValue && t.SomeOtherBoolValue);
Or perhaps:
var predicates = new Func<MyClass,bool>[]{ t => t.SomeBoolValue, t => t.SomeOtherBoolValue};
var listOneFinal = myCollection.Where(predicates.First());
var listFourFinal = myCollection.Where(t => !predicates.Any(p => p(t)));
var listFiveFinal = myCollection.Where(t => !predicates[0](t) && predicates[1](t));
(Call ToList() on each query if desired)
But really, I prefer your technique much better, the code I have provided is not particularly more readable or efficient.
You might want to consider just storing the 2 lookups instead of each list and inline each 'final lookup' where necessary since it's cheap to call Lookup[key]. So whenever you need listFourFinal, just call listThree[false]. Better variable names would help, obviously.
If you find yourself doing this often, you can write a function to do it. For a boolean ToLookup, we can use C#'s out parameters to return multiple values.
public static void Dichotomize<T>(this IEnumerable<T> source,
Func<T,bool> keySelector,
out IEnumerable<T> affirmative,
out IEnumerable<T> negative) {
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
var affirmativeList = new List<T>();
var negativeList = new List<T>();
foreach (var element in source) {
(keySelector(element) ? affirmativeList : negativeList).Add(element);
}
affirmative = affirmativeList.AsReadOnly();
negative = negativeList.AsReadOnly();
}
Now we can do:
IEnumerable<T> listOneFinal, listTwo, listFourFinal, listFiveFinal;
myCollection.Dichotomize(t => t.SomeBoolValue, out listOneFinal, out listTwo);
listTwo.Dichotomize(t => t.SomeOtherBoolValue, out listFiveFinal, out listFourFinal);

Intersect LINQ query

If I have an IEnumerable where ClassA exposes an ID property of type long.
Is it possible to use a Linq query to get all instances of ClassA with ID belonging to a second IEnumerable?
In other words, can this be done?
IEnumerable<ClassA> = original.Intersect(idsToFind....)?
where original is an IEnumerable<ClassA> and idsToFind is IEnumerable<long>.
Yes.
As other people have answered, you can use Where, but it will be extremely inefficient for large sets.
If performance is a concern, you can call Join:
var results = original.Join(idsToFind, o => o.Id, id => id, (o, id) => o);
If idsToFind can contain duplicates, you'll need to either call Distinct() on the IDs or on the results or replace Join with GroupJoin (The parameters to GroupJoin would be the same).
I will post an answer using Intersect.
This is useful if you want to intersect 2 IEnumerables of the same type.
First we will need an EqualityComparer:
public class KeyEqualityComparer<T> : IEqualityComparer<T>
{
private readonly Func<T, object> keyExtractor;
public KeyEqualityComparer(Func<T, object> keyExtractor)
{
this.keyExtractor = keyExtractor;
}
public bool Equals(T x, T y)
{
return this.keyExtractor(x).Equals(this.keyExtractor(y));
}
public int GetHashCode(T obj)
{
return this.keyExtractor(obj).GetHashCode();
}
}
Secondly we apply the KeyEqualityComparer to the Intersect function:
var list3= list1.Intersect(list2, new KeyEqualityComparer<ClassToCompare>(s => s.Id));
You can do it, but in the current form, you'd want to use the Where extension method.
var results = original.Where(x => yourEnumerable.Contains(x.ID));
Intersect on the other hand will find elements that are in both IEnumerable's. If you are looking for just a list of ID's, you can do the following which takes advantage of Intersect
var ids = original.Select(x => x.ID).Intersect(yourEnumerable);
A simple way would be:
IEnumerable<ClassA> result = original.Where(a => idsToFind.contains(a.ID));
Use the Where method to filter the results:
var result = original.Where(o => idsToFind.Contains(o.ID));
Naming things is important. Here is an extension method base on the Join operator:
private static IEnumerable<TSource> IntersectBy<TSource, TKey>(
this IEnumerable<TSource> source,
IEnumerable<TKey> keys,
Func<TSource, TKey> keySelector)
=> source.Join(keys, keySelector, id => id, (o, id) => o);
You can use it like this var result = items.IntersectBy(ids, item => item.id).
I've been tripping up all morning on Intersect, and how it doesn't work anymore in core 3, due to it being client side not server side.
From a list of items pulled from a database, the user can then choose to display them in a way that requires children to attached to that original list to get more information.
What use to work was:
itemList = _context.Item
.Intersect(itemList)
.Include(i => i.Notes)
.ToList();
What seems to now work is:
itemList = _context.Item
.Where(item => itemList.Contains(item))
.Include(i => i.Notes)
.ToList();
This seems to be working as expected, without any significant performance difference, and is really no more complicated than the first.

Categories

Resources