我有幾個表圍繞一個組織表,它只保存一個唯一的ID值。然後,每個組織都可以在特定的地點並擁有特定的名稱。棘手的部分是組織支持位置和名稱更改以及每個更改的指定生效日期。對於這個例子,我有4個相關表格:LINQ to SQL查詢語法
組織:ID(PK,INT,身份)
地點:ID(PK,INT,身份),姓名(VARCHAR),AltLat(浮動),AltLong(浮動)
organization_locations:的organization_ID(FK,INT),位置(FK,INT),eff_date(日期時間)
organization_names:的organization_ID(FK,INT),名稱(NTEXT),eff_date(日期時間),圖標(nvarchar(100))
我需要檢索的是列表所有地點以及特定日期的特定地點的所有組織,並將其投影到我的業務實體中。換句話說,我將爲每個位置提供一個日期,並且需要返回與最近eff_date小於所提供日期的organization_location條目相關的組織。每個組織都有同樣的事情,我需要截至日期的名稱。
這裏是我開始用,但它似乎並沒有工作:
Dim query = From loc In dc.Locations _
Where loc.AltLong IsNot Nothing And loc.AltLat IsNot Nothing _
Select New AnnexA.Entities.AnnexALocation With {.ID = loc.ID, .Name = loc.Location, .Y = loc.AltLat, .X = loc.AltLong, _
.Units = From ol In loc.organization_locations Let o = ol.Organization.organization_names.Where(Function(ed) (ol.eff_date < Date.Parse("1/1/2011"))).OrderByDescending(Function(od) (od.eff_date)).First() Select New AnnexA.Entities.AnnexAMillitaryUnit With {.ID = o.ID, .Name = o.name, .IconPath = o.icon}}
我寧願VB語法,但如果你只能給我一個C#查詢我可以與工作。我嘗試了其他一些變體,但最終我得到了有關預期的「}」或成員不是實體集的一部分的語法錯誤,無論我嘗試使用何種圓括號組合。
「似乎沒有工作」 - 錯誤的結果或編譯器異常?請詳細說明。 – 2009-01-07 16:32:56