2016-08-12 236 views
0

我在數據庫Order和OrderDetails中有兩個表。我在Linq查詢中引用他們,但只從Orders表中提取數據。我也有一個對應的類 - ABOrders:Linq查詢並返回短日期格式的DateTime字段

[Table("[Order]")] 
public partial class ABOrder 
    { 
     [Key] 
     public int OrderID { get; set; } 
     public string Field1 { get; set; } 
     public string Field2 { get; set; } 
     public DateTime OrderDate { get; set; } 
    } 

的問題是,訂購日期帶有日期和時間,我只需要它作爲ShortDate格式的字符串。本來我已經試過:

var result = (from a in ctx.Order 
         join b in ctx.OrderDetails on a.OrderId Equals b.OrderId 
         where a.Field1.Equals(id) && b.Field1.Contains("TEST") 
         select new myClass.ABOrder 
         { 
          OrderId = a.OrderId, 
          Field1 = a.Field1, 
          Field2=a.Field2, 
          OrderDate = String.Format("MM/dd/yyyy", a.OrderDate) 
         }; 

但出錯了,所以我創建了一個新的類:

[Table("[Order]")] 
public partial class CDOrder 
    { 
     [Key] 
     public int OrderID { get; set; } 
     public string Field1 { get; set; } 
     public string Field2 { get; set; } 
     public string OrderDate { get; set; } 
    } 

,並嘗試了以下Linq查詢:

var result = (from a in ctx.Order 
         join b in ctx.OrderDetails on a.OrderId Equals b.OrderId 
         where a.Field1.Equals(id) && b.Field1.Contains("TEST") 
         select new myClass.ABOrder 
         { 
          OrderId = a.OrderId, 
          Field1 = a.Field1, 
          Field2=a.Field2, 
          OrderDate = a.OrderDate 
         }).AsEnumerable().Select(x => new myClass.CDOrder 
         { 
          OrderId = x.OrderId, 
          Field1 = x.Field1, 
          Field2=x.Field2, 
          OrderDate = String.Format("MM/dd/yyyy", x.OrderDate)     
         }); 

現在我得到錯誤:

The entity types 'ABOrder' and 'CDOrder' cannot share table 'Order' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.

何我可以完成我想要的 - 從訂單表中檢索所有數據,但數據格式簡短?

P.S.在向用戶顯示數據時我無法進行任何調整,因此我必須在查詢中執行此操作。

+0

你取回一個DateTime對象。當你真的想把它們顯示給用戶時,只需調用.ToString(「MM/dd/yyyy」) – Steve

+0

我最初試圖做到這一點,但這是事實,當向用戶顯示時我無法做出任何調整 – ElenaDBA

+1

Wouldn' t只是從CDOrder類中刪除屬性'[Table(「[Order]」)]'解決上述問題,還是需要將它識別爲DB的一部分才能使用? – starlight54

回答

0

通過切換到匿名類型解決了它。 Upvoted有用的答案和評論:starlight54 - 移除臺別名形式CDOrder,並建議a.OrderDate.Month + "/" + a.OrderDate.Day + "/" + a.OrderDate.Year

下面盧西恩Bumb是什麼工作:

var result = (from a in ctx.Orders join b in ctx.OrderDetails on a.OrderId Equals b.OrderId where a.Field1.Equals(id) && b.Field1.Contains("TEST") select new { OrderId = a.OrderId, Field1 = a.Field1, Field2=a.Field2, OrderDate = a.OrderDate.Month + "/" + a.OrderDate.Day + "/" + a.OrderDate.Year }).AsEnumerable().Select(x => new myClass.CDOrder { OrderId = x.OrderId, Field1 = x.Field1, Field2=x.Field2, OrderDate = x.OrderDate
});

0

在後端,DateTime是DateTime,它不是字符串。因此,試圖讓DB返回ShortDate是沒有意義的。相反,你可以讓另一隻讀字段

public string OrderDateDate 
{ 
    get 
    { 
     return OrderDate.ToString("MM/dd/yyyy"); 
    } 
} 
1

1號液 - 從數據庫中獲取你想要什麼:

var result=ctx.Order.Where(a=>a.Field1.Equals(id)) 
    .Join(ctx.OrderDetails.Where(b=>b.Field1.Contains("TEST")), 
    x=>x.OrderId ,y=>y.OrderId ,(x,y)=>new CDOrder 
    { 
    OrderId = x.OrderId, 
    Field1 = x.Field1, 
    Field2=x.Field2, 
    OrderDate = x.OrderDate.Month+"/"+x.OrderDate.Day+"/"+x.OrderDate.Year 
    }).ToList(); 

方案二 - 從數據庫中獲取列表訂單並在內存中執行Select語句:

var result=ctx.Order.Where(a=>a.Field1.Equals(id)) 
    .Join(ctx.OrderDetails.Where(b=>b.Field1.Contains("TEST")), 
    x=>x.OrderId ,y=>y.OrderId ,(x,y)=>x) 
.AsEnumerable() 
.Select(x=>new CDOrder 
    { 
    OrderId = x.OrderId, 
    Field1 = x.Field1, 
    Field2=x.Field2, 
    OrderDate = x.OrderDate.ToString("MM/dd/yyyy") 
    }).ToList(); 

在第一個查詢中,我構建了字符串日期,因爲LINQ to Entities不識別方法.ToString()

在第二個查詢中,我使用.AsEnumerable()來獲得List<Order>,然後我在內存中執行select語句。

+0

您的解決方案給了我一個錯誤'實體或複雜類型'myCLass.DAL.CDOrder'無法在LINQ to Entities查詢中構建' – ElenaDBA

+0

@ElenaDBA,請嘗試移除CDOrder '從查詢中返回一個Anonymus類型,這可能是因爲你的類是一個部分的錯誤,如果你有任何錯誤讓我知道! –

+0

已經做到了。謝謝 – ElenaDBA