querying a list of classes

Hi

I have an application that is working, but an operation which is querying a database many times, is performing slowly.. so I need to alter the code to work from memory rather than the database.

I am loading the required data into a list of classes, for sake f example the class contains data for employees.

What I need to do is pull a set of data from the list, as if I was querying a database... for example, I would like to extract a new list which contains all employees with the surname 'Jones'


How can I do this?
cycledudeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Easwaran ParamasivamCommented:
Implement Cache mechanism. You did not mention that is windows or web application?

However please do refer below links:

http://www.codeproject.com/Articles/12097/Cache-Management-in-ASP-NET
http://www.codeproject.com/Articles/7670/How-to-use-data-caching-in-a-NET-Windows-Forms-app

If you want use SQLite.  Refer: http://www.sqlite.org/sharedcache.html

However increasing SP performance would always help you.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>but an operation which is querying a database many times, is performing slowly..
>so I need to alter the code to work from memory rather than the database.

hmmm, I would first check if the query could not be tuned ...
often, just a little small index will work terribly good.

do you have some details?
0
APoPhySptCommented:
if you already have the info loaded into a variable and you want to avoid accessing the database every time, you could just query the list itself with linq.

for example, in c#, after you haded the reference to system.linq, if I'm correct ( If I'm not mistaken most new projects will have the necessary using statment already coded ):

public void Linq2() 
    { 
        List<Product> products = ****Load a products listo to this variable *****
      
        var soldOutProducts = 
            from p in products 
            where p.UnitsInStock == 0 
            select p; 
      
        Console.WriteLine("Sold out products:"); 
        foreach (var product in soldOutProducts) 
        { 
            Console.WriteLine("{0} is sold out!", product.ProductName); 
        } 
    } 

Open in new window


more simple examples and some explanations at microsofts page:
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

käµfm³d 👽Commented:
If you continue down this path, then I would suggest spending some time with LINQ.

http://msdn.microsoft.com/en-us/library/bb397919.aspx
http://msdn.microsoft.com/en-us/library/vstudio/bb397906.aspx

LINQ is composable, so you can build upon queries prior to actually executing them. This means that you can build your where clauses at runtime to give your logic a bit more flexibility.

For your example, if you had a list of employees:

List<Employee> employees = InitializeEmployeeList();

Open in new window


...and your goal was to find all the "Jones" in that list, you could use the Where method:

using System.Linq;

...

List<Employee> employees = InitializeEmployeeList();
var jones = employees.Where(emp => emp.LastName == "Jones");

foreach (var person in jones)
{
    Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
}

Open in new window


You can also use query syntax to accomplish the same:

using System.Linq;

...

List<Employee> employees = InitializeEmployeeList();
var jones = from emp in employees
            where emp.LastName == "Jones"
            select emp;

foreach (var person in jones)
{
    Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
}

Open in new window


With the query syntax, you get something similar to SQL, but it's wholly contained within your C# code**.

LINQ can be a complex topic, and I'm sure you'll have questions. If you decide to proceed with it, feel free to post back or create new questions. There are several knowledgeable LINQ experts hanging around that can get you off on the right foot  = )


** This statement isn't completely true. Above, I am referring to "LINQ-to-Objects". There is also "LINQ-to-SQL". L2S does actually make calls out to the database. I'll save that explanation for another question, though!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cycledudeAuthor Commented:
HI

Sorry i was in a rush when typing the question... It is a c#2010 winforms application, connecting with an access database.

I figured that LinQ would be the way to go...
0
cycledudeAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.