同时获取集合的第一个和最后一个元素

时间:2019-04-29 02:10:45

标签: c# entity-framework mvvmcross

我目前正在从事跨平台项目。

当尝试在WebDataLayer.Models和Shared.Models之间映射模型时,我遇到了问题。

namespace WebDataLayer.Models
{
    public class Factory
    {
        public Guid Id { get; set; }
        public string Name { get; set; }

        public string Serie { get; set; }

        public Guid? AreaId { get; set; }

        public virtual ICollection<FactoryHotline> FactoryHotlines { get; set; }
}

public class FactoryHotline
    {
        public Guid Id { get; set; }
        public Guid FactoryId { get; set; }

        [Required]
        [MaxLength(256)]
        public string Caption { get; set; }

        [Required]
        [MaxLength(256)]
        public string Hotline { get; set; }
    }

这是“共享”中的模型:

namespace Shared.Models
{
    [DataContract]
    public class Factory
    {
        [DataMember(Name = "id")]
        public Guid Id { get; set; }

        [DataMember(Name = "areaId")]
        public Guid AreaId { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "serie")]
        public string Serie { get; set; }

        [DataMember(Name = "hotLine1")]
        public FactoryHotline Hotline1 { get; set; }

        [DataMember(Name = "hotLine2")]
        public FactoryHotline Hotline2 { get; set; }
    }

public class FactoryHotline
    {
        [DataMember(IsRequired = true, Name = "Id")]
        public Guid Id { get; set; }

        [DataMember(Name = "FactoryId")]
        public Guid FactoryId { get; set; }

        [DataMember(Name = "Caption")]
        public string Caption { get; set; }

        [DataMember(Name = "Hotline")]
        public string Hotline { get; set; }
    }
}

这是Controller中的代码,我尝试将WebDatalayer.Models.Factory转换为Shared.Models.Factory

public ActionResult Edit()
        {
            var factories = _factoryService.All().OrderBy(p => p.Name);
            List<Shared.Models.Factory> response = new List<Shared.Models.Factory>();

            response =  factories.Select(k => new Shared.Models.Factory
             {
                 Id = k.Id,
                 Name = k.Name,
                 Serie = k.Serie,
                 Hotline1 = new Shared.Models.FactoryHotline {
                     Id = k.FactoryHotlines.FirstOrDefault().Id,
                     Caption = k.FactoryHotlines.FirstOrDefault().Caption,
                     Hotline = k.FactoryHotlines.FirstOrDefault().Hotline,
                     FactoryId = k.FactoryHotlines.FirstOrDefault().FactoryId
                 },
                 Hotline2 = new Shared.Models.FactoryHotline
                 {
                     Id = k.FactoryHotlines.LastOrDefault().Id,
                     Caption = k.FactoryHotlines.LastOrDefault().Caption,
                     Hotline = k.FactoryHotlines.LastOrDefault().Hotline,
                     FactoryId = k.FactoryHotlines.LastOrDefault().FactoryId
                 },
            }).OrderBy(f => f.Name).ToList();
            return View("Edit", response);
        }

但是linq to entities does not recognize the method lastordefault不能使用order descending,因为我也同时获得了First元素。

需要帮助!

2 个答案:

答案 0 :(得分:1)

您似乎想返回所有按名称排序的工厂,并为每个工厂返回第一个和最后一个热线:

var query = factories.Select(f => new Shared.Models.Factory
    {
        Id = f.Id, 
        Name = f.Name,
        Serie = f.Serie,
        Hotline1 = f.FactoryHotLines
            .OrderBy(h => h.Id)
            .Select(h => new Shared.Models.FactoryHotline 
            {
                Id = h.Id,
                Caption = h.Caption,
                Hotline = h.Hotline,
                FactoryId = h.FactoryId
            }).FirstOrDefault(),
        Hotline2 = x.FactoryHotLines
            .OrderByDescending(h => h.Id)
            .Select(h => new Shared.Models.FactoryHotline 
            {
                Id = h.Id,
                Caption = h.Caption,
                Hotline = h.Hotline,
                FactoryId = h.FactoryId
            }).FirstOrDefault(),
     }).OrderBy(f => f.Name)
     .ToList();

关键在这里:使用FirstOrDefault()时,请始终提供OrderBy子句。与其依靠FirstOrDefault()来获取每个单独的值,不如将其与OrderBy()一起使用来获取实体,然后使用Select()来将其简化为所需的视图模型。

以上示例假定您要添加它们的顺序。 (基于身份的记录的ID)通过这种方式,我们执行OrderBy()获得第一个热线电话,然后执行OrderByDescending()获得第二个热线电话。

答案 1 :(得分:0)

我不确定这是否是最干净的解决方案,但是您可以像这样链接.Where

.Where((v, i) => { return i == 0 || i == list.Count() -1; })

所以..

factories = factories.Where((v, i) => { return i == 0 || i == factories.Count() -1; })