Pages

LINQ To Object --Example -2

Today, we will see how to use Order class in LINQ.

Create a One Class Called : ClsEmployee
Three Properties in the Class.


1)EmpId2)Name3)Salary
public string Name { get; set; } public string EmpID { get; set; } public int Salary { get; set; }
Create a One form
Drag and Drop one Button.
Button name is btn_Result.
Create a Method:
Method name is "InsertintoTable()
List objEmployee = new List { new ClsEmployees { EmpID = "1001" ,Name ="GANTEC", Salary =10000 }, new ClsEmployees { EmpID = "1002" ,Name ="IBM", Salary =15000 }, new ClsEmployees { EmpID = "1003" ,Name ="CTS", Salary =20000 }, new ClsEmployees { EmpID = "1004" ,Name ="WIPRO", Salary =30000 }, new ClsEmployees { EmpID = "1005" ,Name ="SATHYAM", Salary =40000 }, new ClsEmployees { EmpID = "1006" ,Name ="BUTTERFLY", Salary =50000 }, new ClsEmployees { EmpID = "1007" ,Name ="CON", Salary =10000 }, new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 }, new ClsEmployees { EmpID = "1010" ,Name ="TATA", Salary =90000 }, new ClsEmployees { EmpID = "1009 " ,Name ="GANTEC COR", Salary =100000 }, };
Call this method in button Click Event and Add below codes,
var OrderBy = (from emp in objEmployee select emp).OrderBy(x => x.Name);

Happy Coding!!!

LINQ To Object -- Example 1

 Count of Rows in the Table using LINQ
1) Create a One Class Called : ClsEmployee
Three Properties in the Class.
1)EmpId2)Name3)Salary
Code :
   public string Name { get; set; }
   public string EmpID { get; set; }
   public int Salary { get; set; }
2)  Create a One form
Drag and Drop one Button.
Button name is btn_Result.
Create a Method:

Method name is "InsertintoTable()
   List<ClsEmployees> objEmployee = new List<ClsEmployees>
            {
                new ClsEmployees { EmpID = "1001" ,Name ="GANTEC", Salary =10000 },
                new ClsEmployees { EmpID = "1002" ,Name ="IBM", Salary =15000 },
                new ClsEmployees { EmpID = "1003" ,Name ="CTS", Salary =20000 },
                new ClsEmployees { EmpID = "1004" ,Name ="WIPRO", Salary =30000 },
                new ClsEmployees { EmpID = "1005" ,Name ="SATHYAM", Salary =40000 },
                new ClsEmployees { EmpID = "1006" ,Name ="BUTTERFLY", Salary =50000 },
                new ClsEmployees { EmpID = "1007" ,Name ="CON", Salary =10000 },
                new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 },
                 new ClsEmployees { EmpID = "1010" ,Name ="TATA", Salary =90000 },
                 new ClsEmployees { EmpID = "1009" ,Name ="GANTEC COR", Salary =100000 },
             };
Call this method in button Click Event and Add below codes,
 var CountofRows = (from emp in objEmployee select emp.Salary).Count();
 MessageBox.Show("Total Records in Employee Table :" + CountofRows );
Result: "Total Records in Employee Table: 10" in the Message Box

Date Format Directory

Today, we discussed Date Format Directory in C#.Net

Code:
public static void CreateDirectory(string DesignationPath) { if (!Directory.Exists(DesignationPath)) { Directory.CreateDirectory(DesignationPath); } } public static string currentYear() { string Year = string.Empty; DateTime dt = DateTime.Now; string format = "yyyy"; Year = dt.ToString(format); return Year; } public static string currentMonth() { string Month = string.Empty; DateTime dt = DateTime.Now; string format = "MM"; Month = dt.ToString(format); return Month; } public static string currentDate() { string day = string.Empty; DateTime dt = DateTime.Now; string format = "dd"; day = dt.ToString(format); return day; }

Set Excel File Format in C#.Net

Set Excel File Format in C#.net

  public static string ExcelFileType()
        {
            string excelFileType = string.Empty;
            Microsoft.Office.Interop.Excel.Application OfficeFileType = new Microsoft.Office.Interop.Excel.Application();
            OfficeFileType.Visible = false;
            string oVersionName = OfficeFileType.Version;
            int length = oVersionName.IndexOf('.');
            oVersionName = oVersionName.Substring(0, length);
            int versionNumber = int.Parse(oVersionName, CultureInfo.GetCultureInfo("en-US"));
            if (versionNumber >= 12)
            {
                excelFileType = ".xlsx";
            }
            else
            {
                excelFileType = ".xls";
            }

            return excelFileType;
        }

MS Office Version in C#.Net

"MS Office Version in C#.Net" 
public static string GetOfficeVer()
{
string sVersion = string.Empty;
Microsoft.Office.Interop.Excel.Application appVersion = new Microsoft.Office.Interop.Excel.Application();
appVersion.Visible = false;
switch (appVersion.Version.ToString())
{
case "7.0":
sVersion = "95";
break;
case "8.0":
sVersion = "97";
break;
case "9.0":
sVersion = "2000";
break;
case "10.0":
sVersion = "2002";
break;
case "11.0":
sVersion = "2003";
break;
case "12.0":
sVersion = "2007";
break;
case "14.0":
sVersion = "2010";
break;
default:
sVersion = "Too Old!";
break;
}
return sVersion;
}

LINQ Hints

LINQ :
  LINQ -- Language INtegrate Query.
  It supporst all .Net Language.
LINQ -- Data Provider:

  1.    LINQ To Object
  2.   LINQ To ADO.NET
  3.   LINQ To XML


     LINQ To ADO.NET:

  •              LINQ To SQL.
  •              LINQ To Entity Framework.
  •              LINQ To Dataset.

Query Basics:
         Query Expression consists of set of Clauses written in a declarative Syntax similar to SQL /XQuery.
  Query must:

  •      Begin with from Clause.
  •      End with select/Group clause.
  •      Between one or more of following clauses using
  •              Where,Join,from orderby,let,into.

Aggragate Clause:
    Select ,Distinct,Order bymTake,Takewhile,skip,skipwhile,wheremjoinmgroupbymlet,into

Two ways to write LINQ Query:

  1.    Query syntax -- Use Query
  2.    Method Syntax -- Uses Lambda Expression.

Lambda Expression:
    => Goes into Separate Parameters from method body.

LINQ To Object:

  • Object supports either IEnumerable or Ienumerable<t>
  • Array,ArrayList,Collection,Generic Collection.
  • Using System.LINQ namespace.

LINQ To SQL:
  •     Using System.Data.LINQ namespace.
  •     Requires Entity  classes which you can build using the ORD.
  •     ORD-- Object Relational Designer.
  •     E-R data models will use for Other Database.

LINQ To Dataset:

  •       Collections of data from ADO.NET and Put into Dataset or DataTable.

Destroying Threads

Thread.Abort();

Select All string in TextBox using C#.Net

Copy and Paste this Code in your TextBox Keydown Event

if ((e.Control == true) && (e.KeyCode == Keys.A))
{
textBox1.SelectAll();
}

Cannot set a value on node type 'Element'

         Today , we discussed "Cannot set a value on node type 'Element' "
Suppose user are using below code in the below Structure:

TamilNadu private void ChangeValueofXMLNodeValue(string XMLPath,string SelectedSingleTag)
{
XmlDocument doc = new XmlDocument();
doc.Load(XMLPath);
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode(SelectedSingleTag);
myNode.Value = textBox1.Text ;
doc.Save(XMLPath);
}
you will get "Cannot set a value on node type 'Element'." Solution: myNode.Value = textBox1.Text ; change myNode.InnerText = textBox1.Text ;

Set Value for SingleXMLNode usng C#.Net

            Today, we will discuss about "Set Value for SingleXMLNode usng C#.Net"
 Copy and Paste the Following Code

private void ChangeValueofXMLNodeValue(string XMLPath,string SelectedSingleTag) { XmlDocument doc = new XmlDocument(); doc.Load(XMLPath); XmlNode root = doc.DocumentElement; XmlNode myNode = root.SelectSingleNode(SelectedSingleTag); myNode.InnerText = textBox1.Text ; doc.Save(XMLPath); }