none
Newbie Question: Query All? RRS feed

  • Question

  • I can query one record like this:

    Table<Customer> customerTable = dataContext.GetTable<Customer>();
    Customer customerFirstAlbert = customerTable.First(c => c.name == "albert");

    How do I query a collection of records? For example, how could I get all customer records that match a certain critera? I've searched through online docs, I've tried the method "All" (this clearly does something else), I've read dozens of blogs, and I'm sure I'm missing something really simple.

    thanks!
    Monday, May 19, 2008 1:19 AM

Answers

  • Hi Evaristo9,

     

    Some of the useful methods are named as the Transact SQL equivalent, for e.g to query a collection of records you can use Where extension method like this :

     

    Code Snippet

    Table<Customer> customerTable = dataContext.GetTable<Customer>();
    IEnumerable<Customer> customersWithLongNames = customerTable.Where<Customer>(c => c.name.Length >= 6);

     

     

     

    This will return you the collection of customers whose names are longer than or equal to 5 characters. If you want it as a List, you can append ".ToList()" at the end and manipulate it as a list.

     

    Hope this helps, don't hesitate to let us know if you need anything more.

     

    Cheers,

     

    Sidar

     

     

    Monday, May 19, 2008 1:42 AM

All replies

  • Hi Evaristo9,

     

    Some of the useful methods are named as the Transact SQL equivalent, for e.g to query a collection of records you can use Where extension method like this :

     

    Code Snippet

    Table<Customer> customerTable = dataContext.GetTable<Customer>();
    IEnumerable<Customer> customersWithLongNames = customerTable.Where<Customer>(c => c.name.Length >= 6);

     

     

     

    This will return you the collection of customers whose names are longer than or equal to 5 characters. If you want it as a List, you can append ".ToList()" at the end and manipulate it as a list.

     

    Hope this helps, don't hesitate to let us know if you need anything more.

     

    Cheers,

     

    Sidar

     

     

    Monday, May 19, 2008 1:42 AM
  • Evaristo,

    In your exmaple, customerTable is itself a queryable object capable of returning many record. All you need to do is iterate through it:

    Code Snippet

    foreach (var c in customerTable)

        Console.WriteLine(c);

     

     

    If you want to introduce a filtering condition, using function syntax you can do like this:

    Code Snippet
    var customers = customerTable.Where(c=>c.Orders.Count > 1);

     

     

     

    Let’s examine this more in detail:

    ·         var is a new C# keyword that is used to declare a local variable to take advantage of “type inference”. That means that you don’t need to declare the type of the variables in some cases, and the compiler will infer the type from the expression you are assigned to it. The use of “var” is optional, but very convinient. A simple option is to use IEnumerable.

    ·         Where is an extension method that applies to any object that you can enumerate or query (customerTable is such a “queryable” object), and it allows you to specify a condition that returning objects need to meet.

    ·         => is a new notation that is used to define “lambda expressions”. You can interpret this lambda expression as “given a customer called c, c.Orders.Count must be greater than 1”.

    I would recommend reading this article to get a more complete perspective of LINQ in general.

    By the way, the query above is equivalent to this in “LINQ comprehension” syntax:

    Code Snippet

    var customers= from c in customerTable

                   where c.Orders.Count > 1

                   select c;

     

     

     

    Hope this helps,
    Diego

    Monday, May 19, 2008 2:08 AM
  • "Where" is what I was looking for. Thanks both of you!

    I've read several dozen articles that cover lambda expressions, etc, etc, but I was having trouble figuring this specific piece out. Thanks again!
    Monday, May 19, 2008 2:24 AM