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

Wednesday, January 22, 2014

Linq works with IEnumerable <T >

not  IEnumerable
use a cast to get LINQ on a  IEnumerable<T>

Tuesday, January 14, 2014

In LINQ -order by and the Order By

will just use the last order by

use "ThenBy" instead

Thursday, January 2, 2014

question on delegate predicates

in the following class:

class testLinq
    {
       
        public  bool www(string s)
        { return s.Equals("def"); }

        public delegate bool d(string s);

        public void write()
        {
           
            List<String> l = new List<string>() { "abc", "def" };
             
            normal way of doing predicate:  
            Console.WriteLine(l.Find(p => { return p.Equals("def"); }));
            
            bizzare method: 
            Predicate<string> pred = new Predicate<string>(www); 
            Console.WriteLine(l.Find(pred));

            non working method - 
            d di = null;
            di  = www;
            Console.WriteLine(l.Find(di)));
         Why?



            
            Console.ReadLine();


        }


    }

Thursday, August 22, 2013

How to get results in LinqPad

I had to use dump:

var pd = Products.Select(p=>p);
pd.Dump();

Monday, July 29, 2013

Linq Datatable subset as a datatable

blah.Rows.Cast<DataRow>().Where(row => row.Field<int>("ID").Equals(i)).CopyToDataTable();

will return onle where id = i

Thursday, May 9, 2013

Linq to select all rows with data in datable

_data.Tables[tempStr].Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();

Friday, February 22, 2013

Concatenate list with LINQ

listBlah.Aggregate((first, second) => first + "<BR />" + second)

Wednesday, February 13, 2013

Ordering objects with LINQ by multiple properties

I did it thusly



blah.OrderBy(x => x.firstprop).ThenBy(x => x.secondprop).Select(x=>string.Format("{0}:{1}", x.firstprop,x.secondprop)).ToList() ;  

Tuesday, December 4, 2012

Linq issues with datatables

first the interesting thing
if a linq query returns an empty array of datarows - you can still run tolist() on the object.
a different issue that sidetracked me for a long time - if you do something like 


row.Field<string>("BLAH").ToLower()
this will fail if null

Monday, November 5, 2012

Basic linq syntax group by on datatable



            DataTable dt = new DataTable();
            dt.Columns.Add("a", typeof(string));
            dt.Columns.Add("b", typeof(string));
            dt.Columns.Add("c", typeof(string));
            DataRow dr =  dt.NewRow();
            dr[0] = "a - row1";
            dr[1] = DBNull.Value;
            dr[2] = "c -row1";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "a -row2";
            dr[1] = "b -row2";
            dr[2] = "c -row2";

            dt.Rows.Add(dr);

            var subjects = dt.AsEnumerable().GroupBy(row => new { a = row.Field<string>("a"),c=  row.Field<string>("c")}).Select(i => new { Subject = i.Key, Count = i.Count()}) ;
            foreach (var subject in subjects)
            {
                Response.Write(subject.Subject);
            }


results

{ a = a - row1, c = c -row1 }{ a = a -row2, c = c -row2 }
{ a = a - row1, b = , c = c -row1 }{ a = a -row2, b = b -row2, c = c -row2 }

Monday, October 15, 2012

Using Linq with datatables

you need a reference to datasetextensions.

the interesting thing was that was enough to bring up "where" and "order by"clauses but not group by - for that I needed a using system.linq

Wednesday, August 29, 2012

Linq distinct count of a value in a datatable


   var countings  = (from row in ti.Table.AsEnumerable()
                            select row["blah"].ToString()).Distinct();
            if(countings.Count<string>() > 1)
            {

Linq top N from datatable to new datatable


var dtTrec = from item in dt.AsEnumerable()
                                     select item;
                        var topN = dtTrec.Take(20);
                        DataTable dt1 = topN.ToArray().CopyToDataTable<DataRow>();

Friday, March 2, 2012

Lambda Expressions

Does C# absolutely need Lambda's? I dont think so , but it sure helps with LINQ
this is more C# like
PropertyInfo[] p = t.GetProperties();
            IEnumerable<string> names = p.Where(nms => nms.Name.StartsWith("S")).Select(nms => nms.Name);   
than: 
var names = from nms in p
            where nms.Name.StartsWith("S")             
            select nms.Name;