LINQ - Linq using ArrayList - Asked By deepi singh on 02-Apr-12 09:53 AM


example:

class employee

{

  static void Main(string[] args)
{
ArrayList Emp= new ArrayList();
Taxonomy oEmployeeSal = new EmployeeSal();

for (int i = 0; i < 5; i++)
{
oEmployeeSal = new Taxonomy();
oEmployeeSal .TId = i;
oEmployeeSal .TName = "Taxonomy Name -" + i.ToString();
oEmployeeSal .Sal.Add(i.ToString(), ((i + 1) * 10).ToString());
Emp.Add(oTaxonomy);
}
 
foreach (Employeesal item in Emp)
{
for (int i = 0; i < 5; i++)
{
item.sal.Add(i.ToString(), ((i + 1) * 10).ToString());
}
}

 

 

}

 

class Employeesal
{
public int TId { get; set; }
public string TName { get; set; }
public Dictionary<string, int> sal{ get; set; }

now i write an linq query for getting summation of all salaries for tht particular index in arraylist in employee class...

[)ia6l0 iii replied to deepi singh on 02-Apr-12 11:11 AM
By definition, I see that Emp is the Arralylist. 
ArrayList Emp= new ArrayList();

And you seem to be filling it only once - the dictionary object that has the string as the key and int as the value.
oEmployeeSal .Sal.Add(i.ToString(), ((i + 1) * 10).ToString());

So, I am not sure why would you need a LINQ Query to query it. 

However, if you need sum of a property, this is how you would write it.
int sum = Emp.Sum(o => o.sal.Values.Sum());

Hope this helps.
Devil Scorpio replied to deepi singh on 02-Apr-12 04:16 PM
HI,

Here is the linq code using arraylist in C#
// C#
var query = from Student s in arrList

The following example shows a simple query over an ArrayList. Note that this example uses object initializers when the code calls the Add method, but this is not a requirement.

using System;
using System.Collections;
using System.Linq;

namespace NonGenericLINQ
{
    public class Student
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int[] Scores { get; set; }
    }

    class Program
    {
      static void Main(string[] args)
      {
        ArrayList arrList = new ArrayList();
        arrList.Add(
          new Student
            {
              FirstName = "Svetlana", LastName = "Omelchenko", Scores = new int[] { 98, 92, 81, 60 }
            });
        arrList.Add(
          new Student
            {
              FirstName = "Claire", LastName = "O’Donnell", Scores = new int[] { 75, 84, 91, 39 }
            });
        arrList.Add(
          new Student
            {
              FirstName = "Sven", LastName = "Mortensen", Scores = new int[] { 88, 94, 65, 91 }
            });
        arrList.Add(
          new Student
            {
              FirstName = "Cesar", LastName = "Garcia", Scores = new int[] { 97, 89, 85, 82 }
            });

        var query = from Student student in arrList
              where student.Scores[0] > 95
              select student;

        foreach (Student s in query)
          Console.WriteLine(s.LastName + ": " + s.Scores[0]);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
      }
    }
}

/* Output:
    Omelchenko: 98
    Garcia: 97
*/


Reference :- http://msdn.microsoft.com/en-us/library/bb397937.aspx