2009-01-07 94 views
1

我有幾個表圍繞一個組織表,它只保存一個唯一的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#查詢我可以與工作。我嘗試了其他一些變體,但最終我得到了有關預期的「}」或成員不是實體集的一部分的語法錯誤,無論我嘗試使用何種圓括號組合。

+0

「似乎沒有工作」 - 錯誤的結果或編譯器異常?請詳細說明。 – 2009-01-07 16:32:56

回答

0

最後我用下面的代碼得到這個工作:

Dim query4 = From loc In dc.Locations _ 
       Let curNames = (From ons In dc.organization_names _ 
           Where ons.eff_date <= ssDate _ 
           Order By ons.eff_date Descending _ 
           Group ons By ons.organization_id Into gNames = Group _ 
           Select New With { _ 
            Key .Key = organization_id, _ 
            .Result = gNames.Take(1)}) _ 
            .SelectMany(Function(a) (a.Result)) _ 
       Let curLocs = (From oLocs In dc.organization_locations _ 
           Where oLocs.eff_date <= ssDate _ 
           Order By oLocs.eff_date Descending _ 
           Group oLocs By oLocs.organization_id Into gLocs = Group _ 
           Select New With { _ 
           Key .Key = organization_id, _ 
           .Result = gLocs.Take(1)}) _ 
           .SelectMany(Function(a) (a.Result)) _ 
       Where loc.AltLat IsNot Nothing And loc.AltLong IsNot Nothing _ 
       Select New AnnexA.Entities.AnnexALocation With { _ 
        .ID = loc.ID, .Name = loc.Location, .Y = loc.AltLat, .X = loc.AltLong, _ 
        .Units = From curLoc In curLocs _ 
          Where curLoc.location = loc.ID _ 
          From curName In curNames _ 
          Where curName.organization_id = curLoc.organization_id _ 
          Select New AnnexA.Entities.AnnexAMillitaryUnit With { _ 
           .ID = curLoc.organization_id, _ 
           .Name = curName.name, _ 
           .IconPath = curName.icon}} 

感謝您抽出時間來看看這個!

0

我想我明白你想要在這裏做什麼,它看起來像一些簡單的連接可以擺脫你在查詢結束時所做的所有複雜的東西。 (這是在C#中,但它應該是相當直截了當地得到它VB)

from loc in dc.Locations 
join ol in dc.organization_locations on loc.ID equals ol.location 
join orn in dc.organization_names on ol.organization_id equals orn.organization_id 
where loc.AltLong != null 
    && loc.AltLong != null 
    && ol.eff_date < Date.Parse("1/1/2011") 
orderby ol.eff_date 
select new AnnexA.Entities.AnnexAMillitaryUnit 
    { ID = loc.ID, Name = orn.name, IconPath = orn.icon} 

讓我知道這是否正常工作或是否需要任何微調...

0

我需要最新(截至某個特定日期)組織的名稱和考慮組織的名稱。不只是在日期之前。例如,拿美國總統當選奧巴馬。在這個例子中,有三個地方:他的房子,Hay-Adams酒店和白宮。在過去幾個月裏,他也獲得了3個冠軍:奧巴馬參議員,奧巴馬總統,並即將成爲奧巴馬總統。

如果您要將「2008年1月1日」的日期傳遞給此查詢,您應該找回所有3個位置,「參議員奧巴馬」(organization_name)屬於他的「房屋」(organization_location)而另外兩個地點則不應該使用我們的樣本。如果你通過「2008年12月1日」的日期,他仍然在技術上居住在他的家中,但他的名字是「奧巴馬總統」,因此結果將反映這一點。如果你今天通過,他的名字仍然是「奧巴馬總統」,但他的位置變成了「海艾斯酒店」。如果您在「2009年1月20日」之後通過任何日期,他的頭銜將是「奧巴馬總統」,他的位置將是「白宮」。

即使你對政治沒有興趣,或者不是來自美國,我希望這是有道理的。我需要結果來告訴我一切都在哪裏,什麼東西在給定的時間點被稱爲。