同时获取Collection的First和Last元素

问题描述 投票:0回答:2

我目前正在开发一个跨平台项目。

尝试在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,因为我同时也获得了第一个元素。

需要帮忙!

c# entity-framework mvvmcross
2个回答
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()获得第二条热线。


0
投票

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

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

所以..

factories = factories.Where((v, i) => { return i == 0 || i == factories.Count() -1; })
© www.soinside.com 2019 - 2024. All rights reserved.