2010-12-14 36 views
0

我想用Linq创建一个本身包含2个数据对象的对象。从Linq表达式中,我试图直接设置对象,而不是在包含类中包含inbetween函数。Linq使用MVC的许多对象

含类看起来是这样的:

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; 
      } 
     } 

    } 
} 

注:类别和category_sub对象都是通过实体框架建立。

我的链接表达有2个表,看起来像这样:

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 } }, 
          _category_sub = { new category_sub { sub.id, sub.categoryid, sub.name } } 
         }; 

     areaList = results.ToList(); 

     return areaList; 
    } 

希望你可以看到,我想直接创建“_category”和“_category_sub”对象。

我得到的错误是:

错误5无法与集合初始化,因为 它没有实现 'System.Collections.IEnumerable' d初始化 型 'Essential_Collections.Models.category' :\网站\ Essential Collections \ development \ build \ Essential Collections \ Models \ SupplierCategories \ SupplierCategoriesRepository.cs 51 37基本收藏品

任何解决方案或其他可能的方法将受到欢迎

回答

1

尝试从而去除多余的括号:

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; 
} 
+0

非常感谢,做工精细。但是,如果我想创建不直接映射到表的对象,我可以像上面那样指定个别值吗? – Billyhomebase 2010-12-14 16:28:40

+0

@Billyhomebase:@BFree突出了你的类别和category_sub的对象构造函数出错的地方。假如你有一个合适的构造函数,或者你恰当地识别了你正在初始化的属性,那么你可以实例化这些新的对象。 – Lazarus 2010-12-15 12:16:35

+0

如果这个“工作正常”,我建议接受它作为答案。 – 2011-02-02 18:38:15

1

你错过了构造函数调用的括号:

select new SupplierCategories() 
{ 
0
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; 
} 

你得到的错误是因为这些花括号我删除的。这仍然不能编译,因为你需要明确地阐明你的对象初始化设置其属性:

_category = new category { ID = cat.id, Name = cat.name } 

,或者如果您的类别类有一个构造函数重载需要那些指定参数时,再通过这些英寸