Search This Blog

Monday, December 23, 2013

Predicate Delegate C# Example

C# > System > Predicate<T> Delegate

This delegate is used to search for elements in the collection.
Predicate<T> delegate is represented by a lambda expression.

Example


public class Student
{
       public string Name { get; set; }
       public int Year { get; set; }
       public Student(string name, int year)
       {
              this.Name = name;
              this.Year  = year;
       }
}


List<Student> groups = new List<Student>();
groups.AddRange(new Student[] { new Student("Stud1", 1973),
                                new Student("Stud2", 2000),
                                new Student("Stud3", 2010),
                                new Student("Stud4", 1999),
                                new Student("Stud5", 1995) });


foreach (var g in groups.FindAll(x => x.Year >= 2000))
              System.Windows.MessageBox.Show(g.Name);



  





C# System Interfaces

C# > System > Interfaces





IEquatable C# Example

C# > System > Interfaces > IEquatable

IEquatable<T> Interface defines Equals method for determining equality of instances.
Example

public class Car : IEquatable<Car>
{
            public int Power { get; set; }
       public bool Equals(Car other)
       {
              if (other == null)
                    return false;
              if(this.Power == other.Power)
                    return true;
              return false;
       }
       public override bool Equals(Object obj)
       {
              if (obj == null)
                    return false;
              Car carObj = obj as Car;
              if (carObj == null)
                    return false;
              else
                    return Equals(carObj);
       }  
}

Car c1 = new Car();
c1.Power = 100;
Car c2 = new Car();
c2.Power = 100;
if (c1.Equals(c2))
    System.Windows.MessageBox.Show("Same power");





Friday, December 20, 2013

Identify feature DotSpatial Map

DotSpatial > Controls > Map

Identify feature DotSpatial Map on MouseUP event.


private void map1_MouseUp(object sender, MouseEventArgs e)
{
      Coordinate cor =  map1.PixelToProj(e.Location);
      Extent ex = new Extent(cor.X, cor.Y, cor.X, cor.Y);
      var  mLayer = map1.Layers[0];
      IFeatureLayer il = (IFeatureLayer) mLayer;
      List<IFeature> result = il.DataSet.Select(ex);
      if (result.Count>0) 
      {
           IFeature ift = result.FirstOrDefault();
           MessageBox.Show(ift.DataRow[0].ToString());
       }
}





PATINDEX SQL Server Example

SQL Server > Built-in Functions > PATINDEX

PATINDEX returns the start position of pattern occurrence in an expression.

Warning! If pattern or expression is NULL, PATINDEX returns NULL.

Example



SELECT 'This is PATINDEX example' AS 'Expression'

SELECT PATINDEX('%pat%', 'This is PATINDEX example') as 'Find any position pat';

SELECT PATINDEX('%a_p%', 'This is PATINDEX example') as 'Find any a followed by any caracter and then p';












MAX SQL Server Example

SQL Server > Built-in Functions > MAX

MAX returns the maximum value.

Warning! Null values are ignored!

Example

CREATE TABLE #LocalTempTable(
       ID            int,
       Name   varchar(50),
       Salary int,
       DeptId int)

insert into #LocalTempTable(Id, Name, Salary, DeptId) values (1,'p1',100,1),  (2,'p2',200,1) ,  (2,'p3', null,2) ,   (2,'p4', 50,2)

select * from #LocalTempTable

select MAX(Salary) Max_Salary_In_Company from #LocalTempTable
select MAX(Salary) Max_Salary_By_Dept, DeptId from #LocalTempTable group by DeptId

drop table #LocalTempTable









Thursday, December 19, 2013

Retrieve List of Tables in MS Access File C# Example

C# > Data > OLE DB > Access - all tables

Use GetSchema to get all tables in MS Access

Example

OleDbConnection con = new OleDbConnection();

con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=file.mdb;User Id=admin;Password=;";
con.Open();

DataTable userTables = null;
string[] restrictions = new string[4];
restrictions[3] = "Table";

userTables = con.GetSchema("Tables", restrictions);
for (int i = 0; i < userTables.Rows.Count; i++)
   lstTables.Items.Add(userTables.Rows[i][2].ToString());

con.Close();