There are a lot of methods to convert a string/List to a list of something else.
Out of curiosity I can't find if there is a way to do it this way
Directory.EnumerateFiles(#"C:\Drivers").ToList<FileInfo>();
Or Either
new List<string>(Directory.EnumerateFiles(#"C:\Drivers")).Cast<FileInfo>();`
`
As fileinfo takes FileInfo(path) as parameter, there is a way to do like this or a short oneliner which doesn't involve linq Select(x => new FileInfo(x) or something like that?
There is nothing built-in that does this (binding to a constructor). I am not sure why you would want to avoid Select(x => new FileInfo(x)). However, you could if you wanted to define an extension method such as the below Construct to perform the binding:
static void Main(string[] args)
{
const string path = "d:\\";
var results = Directory.EnumerateFiles(path).Construct<string, FileInfo>();
}
private static ConcurrentDictionary<Type, object> constructors = new ConcurrentDictionary<Type, object>();
private static IEnumerable<TOutput> Construct<TInput, TOutput>(this IEnumerable<TInput> input)
{
var constructor = constructors.GetOrAdd(typeof(TOutput), (Type type) =>
{
var parameterExpression = Expression.Parameter(typeof(TInput));
var matchingConstructor = typeof(TOutput).GetConstructor(new[] { typeof(TInput) });
var expression = Expression.Lambda<Func<TInput, TOutput>>(Expression.New(matchingConstructor, parameterExpression), parameterExpression);
return (object)expression.Compile();
});
return input.Select(x => ((Func<TInput,TOutput>)constructor)(x));
}
Related
Hey i am trying to create a class instance, which is immutable object.
So i was thinking to use proxies.
I want each time a developer will try to change the object properties,
a new object will be deep cloned from the current object.
and the result will be this new object with the changes.
its important the class proto will stay available.
Example:
class Men{
constructor(name){
this.schema = {prop: {innerProp:{}}};
this.name = name;
}
set newName(name){
this.name = name;
}
}
var handler = {
set (target, key, value) {
target = new Proxy(_.cloneDeep(target), this);
target[key] = value;
return target // Return the new obj with the change
}
};
let jeson = new Men("jeson");
let jesonProxy = new Proxy(jeson, handler);
// Taking the new jeson proxy with the change
let newJesonProxy = (jesonProxy.schema = {newProp: {newInnerProp: {}}});
Thanks in advance.
No, you cannot use a proxy for this, and setters not either. They do not allow you to change the result of the assignment. Use an ordinary method for creating the changed instances, and simply freeze the objects.
class Man {
constructor(name) {
this.schema = {prop: {innerProp:{}}};
this.name = name;
Object.freeze(this);
}
withName(name) {
return new this.constructor(name);
}
}
const x = new Man("");
const y = x.withName("jeson");
Okay so I have a POCO class that may contain another POCO class as an array. In some instances when I get the data I want to CREATE a list of the lists but not as a level down but all on the same level. I think I am missing something very simple so I thought I would ask here. I keep trying different syntax for Lambdas but the data is there, I can just never make it appear near the top. I would like the solution to be in lambdas if possible instead of doing the old school foreach. I was not sure if you can do this inline at all or if you have to declare a collection first and then add to it. Where I am at:
class Program
{
public class lowerlevel
{
public string ChildName;
}
public class upperlevel
{
public string ItemName;
public lowerlevel[] ChildNames;
}
static void Main(string[] args)
{
// Create a list of a POCO object that has lists in it as well.
List<upperlevel> items = new List<upperlevel>
{
// declaration of top level item
new upperlevel
{
ItemName = "FirstItem",
// declaration of children
ChildNames = new lowerlevel[]
{new lowerlevel {ChildName = "Part1"}, new lowerlevel {ChildName = "Part2"}},
},
// declaration of top level item
new upperlevel
{
ItemName = "SecondItem",
// declaration of children
ChildNames = new lowerlevel[] { new lowerlevel { ChildName = "Part3" } }
}
};
var stuff = items.Select(l1 => l1.ChildNames.ToList().Select(l2 =>
new lowerlevel
{
ChildName = l2.ChildName
}))
.ToList();
// Arghh! I just want to make a new list with lambdas that is NOT nested a level down! This is NOT what I want but it is valid.
stuff.ForEach(n => n.ToList().ForEach(n2 => n2.ChildName));
// I want this but it does not work as I am not doing the expression right
// stuff.Foreach(n => n.ChildName);
}
}
Try using a SelectMany() Rather than .Select()
var stuff = items.SelectMany...
I'm trying to find a string from the List....seems like its not working and if I have just List<string> it does work.. meaning like the below code...
List<string> c = new List<string>();
c.Add("John Doe"));
c.Add("Erich Schulz"));
//I think the problem with the Criterion class?
here is my class structure:
public class Criterion
{
public Criterion(String propertyName, object value)
{
this.PropertyName = propertyName;
this.Value = value;
}
}
//here is the method...
public static List<Criterion> LoadNames()
{
List<Criterion> c = new List<Criterion>();
c.Add(new Criterion("Name1", "John Doe"));
c.Add(new Criterion("Name2", "Erich Schulz"));
return c;
}
here is the code I'm trying to make it work:
bool isExists = LoadNames.Any(s=> "Erich Schulz".Contains(s));
Error:
does not contain a definition for 'Any' and the best extension method overload 'System.Linq.Enumerable.Any<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments
When you call .Contains(s), s isn't a string, it's Criterion. Use .Contains(s.propertyName).
bool isExists = LoadNames().Any(s=> "Erich Schulz".Contains(s.PropertyName));
Also you're using LoadNames as a method, you need to execute it first.
You're attempting to compare a string to a Criterion object, which just doesn't work.
Here is the fixed code:
bool isExists = LoadNames.Any(criterion => String.Equals(criterion.PropertyName, "Erich Schulz", StringComparison.OrdinalIgnoreCase));
I want to return list of typenames from the class ApplicaitonType but i recieve error
Error 1 Instance argument: cannot convert from 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable' C:\Users\sharaman\documents\visual studio 2010\Projects\IssueTracking\BAL_IssueTracking\AppQuery.cs 19 17 BAL_IssueTracking
Please provide your feed back on it...much appericated
public static List AppType()
{
List<ApplicationType> m = new List<ApplicationType>();
var context = new Dll_IssueTracking.IssuTrackingEntities();// Object context defined in Dll_IssuTracking DLL
var query = from c in context.ApplicationTypes//Query to find TypeNames
select new { c.TypeName };
//return query.ToList<ApplicationType>();
m = query.ToList<ApplicationType>();//Error here
return m;
}
Your query selects a sequence of an anonymous type:
select new { c.TypeName };
It's not clear how you're expecting to turn that anonymous type into an ApplicationType.
Given your data source name, I'd expect the method to be as simple as:
public static List<ApplicationType> AppType()
{
using (var context = new Dll_IssueTracking.IssuTrackingEntities())
{
return context.ApplicationTypes.ToList();
}
}
In particular, it's pointless to create an instance of List<ApplicationType> and then ignore it, as you currently do:
List<ApplicationType> m = new List<ApplicationType>();
...
m = query.ToList<ApplicationType>();
return m;
Why would you bother with the variable here, and why would you assign a value to it which you never use?
EDIT: From your comment:
Actually I need to return TypeNames from ApplicaitonType class and bind to Dropdownlist through this function
That suggests your method is declared inappropriately. Either you can return a List<ApplicationType> and specify TypeName as the display part in the data binding, or you can change your method to return a List<string> (assuming that's the type of TypeName):
public static List<string> GetApplicationTypeNames()
{
using (var context = new Dll_IssueTracking.IssuTrackingEntities())
{
return context.ApplicationTypes.Select(type => type.typeName)
.ToList();
}
}
try with this code
public static List<ApplicationType> AppType()
{
var context = new Dll_IssueTracking.IssuTrackingEntities();// Object context defined in Dll_IssuTracking DLL
var query = from c in context.ApplicationTypes//Query to find TypeNames
select new ApplicationType{TypeName = c.TypeName };
return query.ToList();
}
how can I create a dynamic ORDERBY in my LINQ CompiledQuery (e.g. supply Order Field and Direction as parameters for the compiled query)?
I would do it this way, first all you really need is a way to access the property value by string on an object. You could use reflection, but its slow. So use this helper class approach which is based on the tests of http://stefan.rusek.org/Posts/LINQ-Expressions-as-Fast-Reflection-Invoke/3/
public static class LINQHelper
{
public static IComparable OrderByProperty<TClass>(TClass item,
string propertyName)
{
var t = Expression.Parameter(typeof(TClass), "t");
var prop = Expression.Property(t, propertyName);
var exp = Expression.Lambda(prop, t).Compile();
return (IComparable)exp.DynamicInvoke(item);
}
}
The in your code where you want your order by string of property name, in this example col1, you just do the following.
var myQuery = from i in Items
select i;
myQuery.OrderBy(i=>LINQHelper.OrderByProperty(i,"col1"));
Hope this helps.
I think I found it:
Check out this link. It will point you to the VS2008 code samples which contains a Dynamic Linq Query Library which contains the extension method below. This will allow you to go:
Object.OrderBy("ColumnName");
Here is the extension methods, but you may want the whole library.
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) {
return (IQueryable<T>)OrderBy((IQueryable)source, ordering, values);
}
public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) {
if (source == null) throw new ArgumentNullException("source");
if (ordering == null) throw new ArgumentNullException("ordering");
ParameterExpression[] parameters = new ParameterExpression[] {
Expression.Parameter(source.ElementType, "") };
ExpressionParser parser = new ExpressionParser(parameters, ordering, values);
IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
Expression queryExpr = source.Expression;
string methodAsc = "OrderBy";
string methodDesc = "OrderByDescending";
foreach (DynamicOrdering o in orderings) {
queryExpr = Expression.Call(
typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
new Type[] { source.ElementType, o.Selector.Type },
queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters)));
methodAsc = "ThenBy";
methodDesc = "ThenByDescending";
}
return source.Provider.CreateQuery(queryExpr);
}