LINQ to Entities不支持指定的类型成员'TimeBandDescription'

我有看起来像此错误得到错误的代码:LINQ to Entities不支持指定的类型成员'TimeBandDescription'。仅支持初始化程序,实体成员和实体导航属性。我如何使这个AsEnuermable?

public partial class TimeBand
{
    public TimeBand()
    {
    }

    public int Id { get; set; }

    public string Name { get; set; }

    public long From { get; set; }

    public long To { get; set; }

    [NotMapped]
    public string TimeBandDescription => $"{Name} ({TicksToHhMmFormat(From)} - {TicksToHhMmFormat(To)})";

    private static string TicksToHhMmFormat(long ticks)
    {
        if (ticks == long.MaxValue)
        {
            return "Max";
        }
        var timeSpan = new TimeSpan(ticks);
        return $"{(int)timeSpan.TotalHours}{timeSpan.ToString(@"\:mm")}";
    }
}

public class SuplierService : ISuplierService
{
        public SupplierInfoModel GetSupplierList(int supplierId)
            {
                var query = (from s in suppliers
                             join tb in TimeBands on s.fkTimeBandId equals tb.Id
                             where s.supplierId == supplierId
                             select new SupplierInfoModel
                             {
                                 SupplierId = supplierId,
                                 SupplierEmail = s.Email,
                                 Phone = s.Phone,
                                 Fax = s.Fax,
                                 TimeBandDescription = tb.TimeBandDescription,
评论
  • 笑伴孤单
    笑伴孤单 回复

    我有一个可行的解决方案,但没有最漂亮的解决方案。

        join tb in TimeBands on s.fkTimeBandId equals tb.Id
        from tband in timeband.DefaultIfEmpty()
    
    
    
    
                    var Results = (from s in suppliers
                                                 join tb in TimeBands on s.fkTimeBandId equals tb.Id
                                                 where s.supplierId == supplierId
                             select new
                                {
                                    TimeBand = tband,
                                    Supplier = new HotelInfoModel
                                    {
                                                     SupplierId = supplierId,
                                                     SupplierEmail = s.Email,
                                                     Phone = s.Phone,
                                                     Fax = s.Fax,
                                                     //TimeBandDescription = tb.TimeBandDescription, // no longer needed
                                                    . . .
                                }
                            }).ToList();                                    
    
                            foreach (var result in Results)
                            {
                                result.Supplier.TimeBandDescription = result.TimeBand?.TimeBandDescription ?? "No Time Band Desc";
                            }
    
            var suppliers = Results.Select(s => s.Supplier).ToList();