2011-11-03 48 views
13

Tôi biết rằng LINQ to SQL không chuyển đổi DateTime thành string vì không có ToString() trong SQL. Nhưng làm cách nào tôi có thể chuyển đổi DateTime thành chuỗi được định dạng?Chuyển đổi datetime thành chuỗi được định dạng bên trong truy vấn LINQ to SQL

Đây là dòng trong truy vấn sau đây cần giúp đỡ:

StartDate = string.Format("{0:dd.MM.yy}", p.StartDate) 

Toàn bộ truy vấn:

var offer = (from p in dc.CustomerOffer 
      join q in dc.OffersInBranch 
      on p.ID equals q.OfferID 
      where q.BranchID == singleLoc.LocationID 
      let value = (p.OriginalPrice - p.NewPrice) * 100/p.OriginalPrice 
      orderby value descending 
      select new Offer() 
      { 
       Title = p.OfferTitle, 
       Description = p.Description, 
       BestOffer = value, 
       ID = p.ID, 
       LocationID = q.BranchID, 
       LocationName = q.CustomerBranch.BranchName, 
       OriginalPrice = SqlFunctions.StringConvert((decimal)p.OriginalPrice), 
       NewPrice = SqlFunctions.StringConvert((decimal)p.NewPrice), 
       StartDate = string.Format("{0:dd.MM.yy}", p.StartDate) 
      }).First(); 

tôi nhận được thông báo lỗi sau:

LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

Trả lời

10

EDIT: Bây giờ tôi hiểu câu hỏi, tôi đang cho nó một shot :)

var offer = (from p in dc.CustomerOffer 
        join q in dc.OffersInBranch 
         on p.ID equals q.OfferID 
        where q.BranchID == singleLoc.LocationID 
        let value = (p.OriginalPrice - p.NewPrice) * 100/p.OriginalPrice 
        orderby value descending 
        select new 
        { 
         Title = p.OfferTitle, 
         Description = p.Description, 
         BestOffer=value, 
         ID=p.ID, 
         LocationID=q.BranchID, 
         LocationName=q.CustomerBranch.BranchName, 
         OriginalPrice=SqlFunctions.StringConvert((decimal)p.OriginalPrice), 
         NewPrice=SqlFunctions.StringConvert((decimal)p.NewPrice), 
         StartDate=p.StartDate 

        }) 
        .ToList() 
        .Select(x => new Offer() 
        { 
         Title = x.OfferTitle, 
         Description = x.Description, 
         BestOffer=value, 
         ID=x.ID, 
         LocationID=x.BranchID, 
         LocationName=x.CustomerBranch.BranchName, 
         OriginalPrice=x.OriginalPrice, 
         NewPrice=x.NewPrice, 
         StartDate=x.StartDate.ToString("dd.MM.yy") 
        }).First(); 

Tôi biết nó hơi dài nhưng đó là vấn đề với LINQ to SQL.

Khi bạn sử dụng LINQ, lệnh gọi cơ sở dữ liệu sẽ không được thực thi cho đến khi bạn sử dụng một thứ gì đó như ToList() hoặc First() dẫn đến đối tượng thực. Khi cuộc gọi SQL được thực thi bởi lệnh gọi đầu tiên .First(), bạn hiện đang làm việc với các kiểu .NET và có thể sử dụng các công cụ DateTime.

+0

Lỗi: LINQ to Entities không nhận ra phương thức 'System.String ToString (System.String)' và phương thức này không thể được dịch sang biểu thức cửa hàng. – kandroid

+0

Vì vậy, là p.StartDate một DateTime hoặc một chuỗi? – mccow002

+0

p.startdate -is datetime – kandroid

0

nếu đó là ngày giờ bạn cần sử dụng .ToShortDateString(). Nhưng bạn cũng cần khai báo nó AsEnumerable().

var offer = (from p in dc.CustomerOffer.AsEnumerable() 
       join q in dc.OffersInBranch 
        on p.ID equals q.OfferID 
       where q.BranchID == singleLoc.LocationID 
       let value = (p.OriginalPrice - p.NewPrice) * 100/p.OriginalPrice 
       orderby value descending 
       select new 
       { 
        Title = p.OfferTitle, 
        Description = p.Description, 
        BestOffer=value, 
        ID=p.ID, 
        LocationID=q.BranchID, 
        LocationName=q.CustomerBranch.BranchName, 
        OriginalPrice=SqlFunctions.StringConvert((decimal)p.OriginalPrice), 
        NewPrice=SqlFunctions.StringConvert((decimal)p.NewPrice), 
        StartDate=p.StartDate 

       }) 
       .ToList() 
       .Select(x => new Offer() 
       { 
        Title = x.OfferTitle, 
        Description = x.Description, 
        BestOffer=value, 
        ID=x.ID, 
        LocationID=x.BranchID, 
        LocationName=x.CustomerBranch.BranchName, 
        OriginalPrice=x.OriginalPrice, 
        NewPrice=x.NewPrice, 
        StartDate=x.StartDate.ToShortDateString() 
       }).First(); 
21

Một lựa chọn khác là sử dụng SqlFunctions.DateName, mã của bạn sẽ như thế này:

var offer = (from p in dc.CustomerOffer 
       join q in dc.OffersInBranch 
        on p.ID equals q.OfferID 
       where q.BranchID == singleLoc.LocationID 
       let value = (p.OriginalPrice - p.NewPrice) * 100/p.OriginalPrice 
       orderby value descending 
       select new 
       { 
        Title = p.OfferTitle, 
        Description = p.Description, 
        BestOffer=value, 
        ID=p.ID, 
        LocationID=q.BranchID, 
        LocationName=q.CustomerBranch.BranchName, 
        OriginalPrice=SqlFunctions.StringConvert((decimal)p.OriginalPrice), 
        NewPrice=SqlFunctions.StringConvert((decimal)p.NewPrice), 
        StartDate= SqlFunctions.DateName("day",p.StartDate) + "/" + SqlFunctions.DateName("month",p.StartDate) + "/" + SqlFunctions.DateName("year",p.StartDate) 

       }) 

tôi thấy nó hữu ích nếu bạn không muốn cộng thêm chọn khối mới.

+0

Tôi đã phải thêm điều này bằng cách sử dụng: 'using System.Data.Objects.SqlClient;' để sử dụng 'SqlFunctions' – Kristopher

+0

Thêm tham chiếu đến System.Data.Entity.dll nếu bạn dint trước khi sử dụng sqlfunctions – Rama

0

Đó là những gì chúng tôi đã làm, chúng tôi đã thêm một chức năng mới cho các lớp và chúng ta truy vấn ngày như bình thường trong truy vấn:

[ComplexType] 
public class Offer 
{  
    public DateTime StartDate 
    { 
     get; 
     set; 
    } 

    public String Title 
    { 
     get; 
     set; 
    } 

    /*Other fields*/  
    . 
    . 
    . 


    public string FormattedDate(string format) 
    { 
     return Date.ToString(format); 
    } 
} 



var offer = (from p in dc.CustomerOffer 
     join q in dc.OffersInBranch 
     on p.ID equals q.OfferID 
     where q.BranchID == singleLoc.LocationID 
     let value = (p.OriginalPrice - p.NewPrice) * 100/p.OriginalPrice 
     orderby value descending 
     select new Offer() 
     { 
      Title = p.OfferTitle, 
      Description = p.Description, 
      BestOffer = value, 
      ID = p.ID, 
      LocationID = q.BranchID, 
      LocationName = q.CustomerBranch.BranchName, 
      OriginalPrice = SqlFunctions.StringConvert((decimal)p.OriginalPrice), 
      NewPrice = SqlFunctions.StringConvert((decimal)p.NewPrice), 
      StartDate = p.StartDate 
     }).First(); 

Sau đó, bạn chỉ có thể gọi cho lĩnh vực FormattedDate đi qua các định dạng mong muốn.

edit1.Text = offer.FormattedDate("dd.MM.yy"); 

Hoặc có thể có thể định nghĩa nó như là một lĩnh vực chỉ với các getter:

public string FormattedDate 
       { 
        get { return Date.ToString("dd.MM.yy") }; 
       } 

edit1.Text = offer.FormattedDate; 

Trong trường hợp bạn lớp là một thực thể, bạn cần phải khai báo một phần mới của lớp đó và thêm lĩnh vực này.

Hy vọng điều này sẽ giúp ai đó.

Các vấn đề liên quan