LINQ错误无效的初始成员声明 [英] LINQ error invalid initializer member declarator

查看:325
本文介绍了LINQ错误无效的初始成员声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相当初级C#开发人员,所以原谅我,如果这是微不足道的,但我在code线下面我指出来了这个错误无效的初始成员声明。

谁能解释这是什么意思?如何解决/实现这一结果?我所有的数据注解的PODetail已经应用过一次,这就是为什么我不想在这里重复的属性也是如此。

 公共类PODetailsListViewModel:IViewModelList< PODetail,PODetailsListViewModel>中IEntity
    {
        公众诠释标识{搞定;组; }        公共字符串EntityDescription
        {
            得到
            {返回this.Id.ToString(); }
        }        公共PODetail PODetail {搞定;组; }
        公共IEnumerable的< PODetailsListViewModel> ConvertClassToViewModel(IEnumerable的< PODetail> poDetails)
        {
            IEnumerable的< PODetailsListViewModel> contactGrid =
                从l在poDetails.ToList()
                选择新PODetailsListViewModel()
                {
                    ID = l.Id,
                    PODetail.POHeaderId = l.POHeaderId,< ===== ERROR在此行
                    ....
                };            返回contactGrid;
        }


解决方案

您不能在类初始化像访问复杂对象的属性(如你是用 PODetail.POHeaderId 属性)。您可能需要实现一个小解决方法:使用信息的匿名对象的集合,你已经有,那么重建的 PODetailsListViewModel 没错之后的具体名单:

  VAR contactGrid =由升的poDetails.ToList()
                  选择新{
                  {
                       ID = l.Id,
                       POHeaderId = l.POHeaderId
                       ...
                  };

然后你就可以手动重新创建你在找什么:

  VAR someNewCollection =新名单(中PODetailsListViewModel);
的foreach(在contactGrid VAR项)
    VAR模型=新PODetailsListViewModel()
    {
         ID = item.Id
    };
    model.PODetail =新PODetailModel()/ *实际的类名称替换* /
    {
        POHeaderId = item.POHeaderId
    };
    someNewCollection.Add(模型);
}
返回someNewCollection;

修改:或许你可以窝在初始化:

  VAR contactGrid =由升的poDetails.ToList()
                  选择新PODetailsListViewModel(){
                  {
                       ID = l.Id,
                       PODetail =新PODetailModel()
                       {
                           POHeaderId = l.POHeaderId
                       },
                       ...
                  };

I'm a fairly junior C# developer so excuse me if this is trivial, but I am coming up with this error "invalid initializer member declarator" at the line of code I indicate below.

Can someone explain what this means and how do I work around/achieve this result? All of my data annotations are already applied once in PODetail, which is why I didn't want to repeat the properties here as well.

public class PODetailsListViewModel : IViewModelList<PODetail, PODetailsListViewModel>, IEntity
    {
        public int Id { get; set; }

        public string EntityDescription
        {
            get
            { return this.Id.ToString(); }
        }

        public PODetail PODetail { get; set; }


        public IEnumerable<PODetailsListViewModel> ConvertClassToViewModel(IEnumerable<PODetail> poDetails)
        {
            IEnumerable<PODetailsListViewModel> contactGrid =
                from l in poDetails.ToList()
                select new PODetailsListViewModel()
                {
                    Id = l.Id,
                    PODetail.POHeaderId = l.POHeaderId, <===== ERROR on this Line
                    ....
                };

            return contactGrid;
        }

解决方案

You can't access complex object properties in a class initializer like that (as you are attempting with the PODetail.POHeaderId property). You might have to implement a small workaround: create a collection of anonymous objects with the information you have already, then reconstruct a concrete list of PODetailsListViewModels right afterwards:

var contactGrid = from l in poDetails.ToList()
                  select new {
                  {
                       Id = l.Id,
                       POHeaderId = l.POHeaderId
                       ...
                  };

And then you can manually recreate what you're looking for:

var someNewCollection = New List(Of PODetailsListViewModel);
foreach (var item in contactGrid)
    var model = new PODetailsListViewModel()
    {
         Id = item.Id
    };
    model.PODetail = new PODetailModel() /* replace with actual class name */
    {
        POHeaderId = item.POHeaderId
    };
    someNewCollection.Add(model);
}
return someNewCollection;

EDIT: Or perhaps you could nest the initializers:

var contactGrid = from l in poDetails.ToList()
                  select new PODetailsListViewModel() {
                  {
                       Id = l.Id,
                       PODetail = new PODetailModel()
                       {
                           POHeaderId = l.POHeaderId
                       },
                       ...
                  };

这篇关于LINQ错误无效的初始成员声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆