0
public static IEnumerable<Student> GetStudents()
{
    List<Student> students = (List<Student>)HttpContext.Current.Cache.Get("GetAllStudentWithImg");

    if (students!= null)
    {
        return students ;
    }

    students = new List<Student>();

    using (PrincipalContext context = GetPrincipalContext())
    {
        using (UserPrincipal uprinc = new UserPrincipal(context))
        {
            using (PrincipalSearcher psearcher = new PrincipalSearcher(uprinc))
            {
                students = psearcher.FindAll()
                                    .Select(x => new Student((DirectoryEntry)x.GetUnderlyingObject()))
                                    .ToList(); 
            }
        }
    }

    HttpContext.Current.Cache.Add("GetAllStudentWithImg", students, null, 
                                  DateTime.UtcNow.AddMinutes(60), 
                                  System.Web.Caching.Cache.NoSlidingExpiration,
                                  System.Web.Caching.CacheItemPriority.Normal, null);

    return students;
}

I created this function to get values from my database and it work but it load slowly, the pictures are not that many or big, so its not the content, I wonder if someone want to help me with improving my code?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • suggestion,, have two calls to load basic data and image related data.. – Kavindu Dodanduwa May 07 '16 at 07:28
  • That's not loading from a *database* - this code is searching **Active Directory**..... question is: ***why*** do you need to get the underlying object? That's a rather expensive call - can you do without that call? – marc_s May 07 '16 at 08:07
  • @marc_s Active Directory... Where? – lal May 07 '16 at 10:01
  • @Lal: `PrincipalContext`, `PrincipalUser` - those are classes in .NET to access and work with **Active Directory** .... – marc_s May 07 '16 at 14:31
  • @marc_s Thanks :) , i thought they were just Student, Principal, School classes etc. – lal May 10 '16 at 07:56

3 Answers3

1

I do not see all your code, the code where you query to the database is not displayed, but I think the problem is here:

psearcher.FindAll().Select( ....

FindAll() returns all records in the database table, after this you select what you need in your collection.

The better performance solution is: to query to the database only what you need.

then do not FIND ALL data, but find only what you need

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
danilonet
  • 1,757
  • 16
  • 33
  • Actually, he's not filtering at all. In this case he's just using Select to transform the results, not actually to select them. – Paul May 07 '16 at 09:24
  • Thansk.. that was the one i was looking after, my biggest problem was the photos. – GhostReccon May 10 '16 at 09:14
1

Check this, comparision between findall and where: C# FindAll VS Where Speed.

Beside that, why don't you use inheritance between these PrincipalContext,UserPrincipal and PrincipalSearcher?

Community
  • 1
  • 1
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
0

FindAll() is not a Linq method. It's List(Of T) method and as such, it does not have Deffered execution as opposed to Where() which is Linq and supports deferred execution.

Mukesh Adhvaryu
  • 642
  • 5
  • 16