views:

74

answers:

2

Hi I have a method named "GetOrders". This method reads an XML file and gathers OrderNumber of all orders related to a specific user. The method returns a list.

Now I wanna to know, having my list, How can I get other fields of these OrderNumbers from DB?

+1  A: 

If you use LINQ to SQL, it is trivial. I'll assume that you already have the designer-generated tables and your list (though why you're keeping it in XML is a curiosity, it seems more reasonable to keep it in the database as well).

 var orderNumbers = ... your list ...

 using (var dataContext = new OrdersDataContext()) // designer-generated context
 {
     var orderTitles = dataContext.Orders
                                  .Where( o => orderNumbers.Contains( o.OrderNumber ) )
                                  .Select( o => o.OrderTitle );

     ... now do something with the collection...
 }
tvanfosson
could you please explain a little about your OrdersDataContext?
odiseh
You probably need to read up on LINQ to SQL. The DataContext is (typically) generated by the designer when you add a LINQtoSQL class to your project. You can the use the server explorer to add tables from your database. This results in autogenerated classes corresponding to the tables you add. These are contained in the data context. A good place to start is Scott Guthrie's blog: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
tvanfosson
many thanks to you.
odiseh
A: 

GetOrders needs to de-serialize the Orders into Order Objects and return these (not just the Order Numbers) So the List (Return Type) you want returned by GetOrders() is List.

eg; List orders = GetOrders();

Then you can iterate through the Orders and get the other fields eg:

foreach(Order order in orders) { string orderNumber = order.OrderNumber; Guid id = order.ID; }

Mark Redman