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

Monday, June 1, 2015

Advanced LINQ

Get same attribute between 2 List

var listPerson = AlllistPerson.Where(x => listPhone
 .Any(y => x.PersonPhone.Contains(y.PhoneNmbers).ToList();

Remove from another List (duplicate)

var result = personList.Where(c => !transactionList.Exists(b => c.phone == b.phone)).ToList();

Remove from another List (duplicate) & Child List


List<long> phoneNumbers = sessionTelco.SelectMany
                            (x => x.PhoneList.Select(y => y.phone)).ToList();

coveragePrices = coveragePrices.Where(x => ! phoneNumbers.Contains(x. phone)).ToList();

Selecting First Item from every group

var grouped = personList.GroupBy(x => x.CountryName).Select(y => y.First()).ToList();

 var grouped = from x in personList
            group x by x.CountryName
            into y
            select y.First();


select many from child list.


List<long> routeClassIDList = _upc.SessionClientGroupList.SelectMany
         (x => x.ClientGroupCoverageList.Select(y => y.RouteClassID)).ToList();


filter all child list same with list


clientGroups = clientGroups.Where(x => !x.ClientGroupCoverageList.All(y => routeClassIDList.Contains(y.RouteClassID))).ToList();


ForEach Add if else


people.ForEach(x => { if (x.Balance.HasValue == false || x.Balance.Value == 0) x.Balance = null; });


INNER JOIN 
devicePerAccList = (from f in devicePerAccList
                    join s in Accounts on f.AccountId equals s.E2AccountId
                    select new DevicePerAcc
                    {
                        AccountId = f.AccountId,
                        AccCode = s.AccountCode,
                        NoOfDevice = f.NoOfDevice

                    }).ToList();

Select Many, Find Description, Filter Description = null, Order By 1A , 2B , 11A

preBookMealList = ssrReportList.SelectMany(a => a.SSRCodes.Select(b => new PreBookMeal
{
SSRCode = b,
SSRDescription = ssrMealList.Where(c => c.SSRCode == b).Select(d => d.Name).FirstOrDefault()
})
.Where(e => e.SSRDescription != null))
.OrderBy(f => int.Parse(f.SeatNo.Remove(f.SeatNo.Length - 1)))
.ToList();


Dictionary put Key to object ID

result = peopleList.Select(x => { x.Value.peopleID = x.Key; return x.Value; }).ToList();

Wednesday, December 10, 2014

find the different between 2 list

//DELETE
var oldDifferentList = oldSchool.Students.Except(newSchool.Students, new StudentComparer()).ToList();

//ADD 
var newDifferentList = newSchool.Students.Except(oldSchool.Students, new StudentComparer ()).ToList();

//EDIT

var statusChangedList = oldSchool.Students.Union(newSchool.Students).GroupBy(x => x.StudentID).Where(y => y.First().Status!= y.Last().Status).ToList();



//COMPARER
public class StudentComparer : IEqualityComparer<Student>
{
        public bool Equals(Student p1, Student p2)
        {
            if (Object.ReferenceEquals(p1, p2))
                return true;

            return p1.StudentID == p2.StudentID && p1.City == p2.City;
        }

        public int GetHashCode(Student obj)
        {
            if (Object.ReferenceEquals(obj, null)) return 0;

            int hashStudentID = obj.StudentID.GetHashCode();
            int hashCity = obj.City.GetHashCode();


            return hashStudentID ^ hashCity;
        }
}

Tuesday, July 22, 2014

Object List Reference Not Equal

Not Equal - Although is not equal , but when update it still will reference

List
<Person> personList1 = new List<Person>(upc.personList2.ToList());

Not Equal and Not Reference

List<Person> person = upc.personList2.Select(x => new Person
{
     Name = x.Name,
     Address = x.Address,
     Phone = phone,
}).ToList();

Monday, May 26, 2014

LINQ GET MIDLE RECORD (PAGING)

 var pagesize = Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]);
                
result = result.Skip(pagesize * currentPage).Take(pagesize).ToList();

LINQ ORDER BY EXPRESSION PARAM

var param = Expression.Parameter(typeof(PERSON));
                
Func<PERSON, Object> sortExpression = Expression.Lambda<Func<AuditLogs, Object>>(Expression.Convert(Expression.Property(param, sortColumn), typeof(Object)), param).Compile();

if (sort == "DESC")
  result = result.OrderByDescending(sortExpression).ToList();
       else
  result = result.OrderBy(sortExpression).ToList();


OTHERS METHOD


public Func<T, Object> GetExpression<T>(String sortby)
{
  var param = Expression.Parameter(typeof(T));
  var sortExpression = Expression.Lambda<Func<T, Object>>(Expression.Convert(Expression.Property(param, sortby), typeof(Object)), param);
  
  return sortExpression.Compile();

}

  result = result.OrderBy(GetExpression<PERSON>(sortColumn)).ToList();

Friday, May 23, 2014

LINQ Distinct with a property

Comparer(No work)


Group By Method(work)
result = result.GroupBy(cust => cust.Name).Select(grp => grp.First()).ToList();