Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

distinct by field LINQ c# use MoreLinq

Posted by Unknown 0 comments
Distinct by field in  LINQ c# use MoreLinq

C# enhances Linq to Objects implementation in distinct method.which we can get values distinctly by a filed name in enumerable by using distinctBy method.this extention method we can get by the nuget packages
you can down load the extensions from this link click Here to get dll

Example :Mylist..DistinctBy(x => x.Name).ToList(); 

the inner implementation determines by projection and equality comparer of the projected type

in order to use this package we need to get the reference of

MoreLinq.Source.MoreEnumerable.DistinctBy

 Hope this will helps you
Labels: ,

LINQ sorting using Lambda Expressions with dynamic field names

Posted by Unknown 0 comments

LINQ sorting using Lambda Expressions with dynamic field names

In this blog am going to explain you how sort the Linq collection by the dynamic field names
in order to archive this we nee to use reflection

Here is the small code block will overrides the order by functionality to pass filed names dymamically


 public static IEnumerable<T> OrderBy<T>(  
     this IEnumerable<T> collection,   
     string columnName  
     )  
 {  
   ParameterExpression param = Expression.Parameter(typeof(T), "y");  // y  
   Expression property = Expression.Property(param, columnName);    // y.ColumnName  
   Func<T, object> lambda = Expression.Lambda<Func<T, object>>(    // y => y.ColumnName  
       Expression.Convert(property, typeof(object)),  
       param)  
     .Compile();  
   Func<IEnumerable<T>, Func<T, object>, IEnumerable<T>> expression = (c, f) => c.OrderBy(f);   
   IEnumerable<T> sortedlist = expression(collection, lambda);  
   return sortedlist;  
 }  


Based on the above function we can use as bellow


IEnumerable<T> Mylist= ...
IEnumerable<T> orderedList = Mylist.OrderBy("PropName");



Hope this will helps you

Labels: ,

Handling large SQL queries with LINQ

Posted by Unknown 0 comments
Labels: ,

create the dynamic object list in c# (Expando object)

Posted by Unknown 0 comments
create the dynamic object list in c# (Expando object)

In this post am going to explain about creating dynamic object list using Expando Object

See the bellow implementation

var DynamicObjList = new List<dynamic>();
dynamic obj = new ExpandoObject();
obj.someDynamicFiledname= "some value"; 

If you want to add field name also dynamically use bellow

var objDictionary = obj as IDictionary<string, object>;
objDictionary .Add("your filedname","value");
objDictionary =objDictionary .ToDictionary(x => x.Key, x => x.Value);
DynamicObjList.Add(objDictionary );


hope this will helps you
Labels: ,
 
test