-1

In my code, I need help to put result value into oList

NorthwindDataContext db = new NorthwindDataContext();
List<Category> oList = new List<Category>();
var result = from p in db.Categories
         select new { CategoryID = p.CategoryID, CategoryName=p.CategoryName };

I want oList filled by result values.

Pranay Rana
  • 164,177
  • 33
  • 228
  • 256
shamim
  • 6,186
  • 19
  • 71
  • 139

3 Answers3

1

Your result doesn't have the entire object that is required in the list. So you can do the following instead.

 NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = new List<Category>();
            oList.AddRange(db.Categories);

If adding to list is not strictly required then you can simply convert the result set to list like so:

 NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = db.Categories.ToList();

You however need to know that this is sample code. Pulling the entire table like this is not probably the best thing to do (unless you know there will be fixed no. of records in table that won't change and its safe to load them all in memory).

Muhammad Hasan Khan
  • 33,106
  • 14
  • 80
  • 125
  • thanks for reply,i know this process ,problem aries when i write the select new{} on my linq.How to solve this issue – shamim Oct 10 '11 at 06:29
  • @shamim: Just don't do that! If you only want a subset of the values, leave it as an anonymously typed list. If you want full Category objects, don't fetch a subset of the properties... – Jon Skeet Oct 10 '11 at 06:32
0
NorthwindDataContext db = new NorthwindDataContext();
        List<Category> oList = db.Categories.ToList();

Edit:

Assuming that Category class is your own

NorthwindDataContext db = new NorthwindDataContext();
        List<Category> oList = db.Categories.Select(p => new Category { CategoryID = p.CategoryID, CategoryName=p.CategoryName }).ToList();
Pranay Rana
  • 164,177
  • 33
  • 228
  • 256
Piotr Auguscik
  • 3,561
  • 1
  • 20
  • 28
0

Currently result is a collection of an anonymous type, you want it to return you a Category instead.

NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = new List<Category>();
            var result = from p in db.Categories
                         select new Category { CategoryID = p.CategoryID, CategoryName=p.CategoryName };

You can then add your Categories to oList -

oList.AddRange(result.ToList());

EDIT:

Ok, given that you only want to get a few fields from the database, create a new type with only those fields (if you don't need to use it outside your method, you won't have to do this, just leave it as an anonymous type) -

class CategorySml
{
  public int CategoryID {get; set;}
  public string CategoryName {get; set;}
}

...

NorthwindDataContext db = new NorthwindDataContext();
            List<CategorySml> oList = new List<Category>();
            var result = from p in db.Categories
                         select new CategorySml { CategoryID = p.CategoryID, CategoryName=p.CategoryName };
Frank Tzanabetis
  • 2,396
  • 2
  • 19
  • 22
  • i want a list of category table.This table contain many columns,i want to select not all columns.How to solve this issue – shamim Oct 10 '11 at 06:31
  • i already try addrange() ,but show me error cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable' – shamim Oct 10 '11 at 06:34
  • You probably should have mentioned that in your question, because that makes the question quite different. – Frank Tzanabetis Oct 10 '11 at 06:35
  • "i already try addrange()" - addrange will only work if you have "select new Category {" instead of "select new {". – Frank Tzanabetis Oct 10 '11 at 06:41