2011-04-01 79 views
0

更新使用查看數據加入查詢

我現在有它的工作程度的視圖填充,但我的查詢是錯誤的,因此沒有數據檢索。

我的視圖模型

public class ViewProductions 
{ 

    public string Venuename { get; set; } 
    public string Showname { get; set; } 
    public DateTime ProductionYear { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 


} 

查詢

var query = 
        from f in _db.Production 
        join g in _db.Run on f.show equals g.venue 
        select new ViewProductions { 
          Venuename = g.venue, 
          Showname = f.show, 

          StartDate = g.startDate, 
          EndDate = g.endDate 


        }; 

     return View(query); 

我在SQL格式的查詢

SELECT Production.show, Run.venue, Run.startDate, Run.endDate, Production.director, Production.designer 
FROM Production INNER JOIN 
Run ON Production.show = Run.show 

誰能幫我將其轉換成LINQ?

再次感謝

回答

1

這是一個好的開始。記住這個想法是ViewModel將包含視圖所需的所有數據。使其儘可能大或小。

如果你將顯示顯示列表,你可能需要將其更改爲類似:

public List<Show> {get; private set;} 

如果在給定的對象,你只能使用一個屬性。不要擔心創建ViewData屬性而不是使用整個對象。舉個例子:

public class Venue 
{ 
    public string Name; 
    public string State; 
    public string City; 
    public int Capacity; 
    ... 
} 

假設這個給定的ViewModel只需要Venue的名字。無需將整個Venue添加到它,只需添加一個VenuName屬性即可。

A blog有一個很好的解釋。

EDIT

Here是使用System.Data.Linq.SqlClient.SqlMethods具體地說,DATEDIFF一些很好的例子。你可以做這樣的事情:

where SqlMethods.DateDiffMonth(Book.EntryDate, DateTime.Now) <= 3 order by dates.startDate 

EDIT 2

嘗試是這樣的:

兩個指針,注意ViewModelProduction(),你必須把()以表明您正在創建新對象。 ToList()用於轉換LINQ返回的IEnumerable。同樣在這種情況下,我在startDate上使用orderby。看看一些LINQ examples。而這個非常有用的工具:LinqPad

var query = (from p in db.Production 
      join r in _db.Run on p.show equals r.show 
      orderby r.startDate 
      select new ViewModelProduction() 
      { 
       show = p.show, 
       venue = r.venue, 
       startDate = r.startDate, 
       endDate = r.endDate, 
       director = p.director, 
       designer = p.director 
      }).ToList(); 
+0

感謝您的信息@mcbros。我更新了問題,詢問我需要創建的查詢,因爲我遇到了一些問題 – 2011-04-02 14:38:41

+0

@Chris_,我編輯了一些更多信息,或者我希望如此。 – mateuscb 2011-04-02 15:35:22

+0

感謝您的幫助,我快到了!我已經更新了答案,基本上這是我需要排序的查詢,然後應該正常工作。 – 2011-04-03 20:29:48