I am trying to use Linq to create an object which itself contains 2 data objects. From the Linq expression I am trying to set up the objects directly rather than having an inbetween function in the containing class.
The containing class looks like this:
public class SupplierCategories
{
public category _category;
public category_sub _category_sub;
private bool _IsActive;
private int? _SupplierID;
public SupplierCategories()
{
}
public bool IsActive
{
get
{
return _IsActive;
}
set
{
_IsActive = value;
}
}
public int? SupplierID
{
get
{
return _SupplierID;
}
set
{
_SupplierID = value;
if (_SupplierID != null) // The county is active if there is a supplier ID matching
{
_IsActive = true;
}
}
}
}
Note: category and category_sub objects are all set up through the Entity Framework.
My link expression has 2 tables and looks like this:
public IEnumerable<SupplierCategories> GetAllOrdered()
{
IEnumerable<SupplierCategories> areaList;
var results = from cat in dbContext.categories
join sub in dbContext.category_sub on cat.id equals sub.categoryid
orderby cat.id ascending, sub.name ascending
select new SupplierCategories
{
_category = { new category { cat.id, cat.name } },
_c开发者_如何学JAVAategory_sub = { new category_sub { sub.id, sub.categoryid, sub.name } }
};
areaList = results.ToList();
return areaList;
}
Hopefully you can see that I am trying to create the "_category" and "_category_sub" objects directly.
The error I get is:
Error 5 Cannot initialize type 'Essential_Collections.Models.category' with a collection initializer because it does not implement 'System.Collections.IEnumerable' D:\Websites\Essential Collections\development\build\Essential Collections\Models\SupplierCategories\SupplierCategoriesRepository.cs 51 37 Essential Collections
Any solutions or possibly other approaches would be gratefully received
Try removing the extra braces thus:
public IEnumerable<SupplierCategories> GetAllOrdered()
{
IEnumerable<SupplierCategories> areaList;
var results = from cat in dbContext.categories
join sub in dbContext.category_sub on cat.id equals sub.categoryid
orderby cat.id ascending, sub.name ascending
select new SupplierCategories()
{
_category = cat,
_category_sub = sub
};
areaList = results.ToList();
return areaList;
}
You missed the parantheses on the constructor call:
select new SupplierCategories()
{
public IEnumerable<SupplierCategories> GetAllOrdered()
{
IEnumerable<SupplierCategories> areaList;
var results = from cat in dbContext.categories
join sub in dbContext.category_sub on cat.id equals sub.categoryid
orderby cat.id ascending, sub.name ascending
select new SupplierCategories
{
_category = new category { cat.id, cat.name } , //removed the curly braces here
_category_sub = new category_sub { sub.id, sub.categoryid, sub.name } //removed the curly braces here
};
areaList = results.ToList();
return areaList;
}
The error you're getting is because of those curly braces I removed. This still won't compile because you need to explicitly spell out which property you're setting in the object initializer:
_category = new category { ID = cat.id, Name = cat.name }
or if your category class has a constructor overload that takes those args, then pass those in.
精彩评论