LINQ to SQL查询语法

时间:2009-01-07 16:25:01

标签: vb.net linq linq-to-sql

我有几个以组织表为中心的表,它只包含唯一的ID值。然后,每个组织可以位于特定位置并具有特定名称。棘手的部分是组织支持位置和名称更改以及每次更改的指定生效日期。在这个例子中,我有4个相关的表:

组织:ID(PK,int,identity)

位置:ID(PK,int,identity),Name(varchar),AltLat(float),AltLong(float)

organization_locations:organization_id(FK,int),location(FK,int),eff_date(datetime)

organization_names:organization_id(FK,int),name(ntext),eff_date(datetime),icon(nvarchar(100))

我需要检索的是所有位置的列表以及特定日期的给定位置的所有组织,并将它们投影到我的业务实体中。换句话说,我将提供一个日期并需要返回每个位置,组织与organization_location条目相关,最新的eff_date小于提供的日期。对于每个组织都是一样的,我需要在日期之前的名称。

这是我开始使用但它似乎不起作用:

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#查询,我可以使用它。我尝试过其他一些变体,但最终我得到关于预期的“}”的语法错误或者成员不是实体集的一部分,无论我尝试使用哪种括号组合。

3 个答案:

答案 0 :(得分: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}

让我知道这是否有效或是否需要微调......

答案 1 :(得分:0)

我需要考虑组织和组织名称的最新(截至给定日期)位置。不仅仅是在约会之前。例如,以美国总统当选奥巴马为例。在这个例子中有3个地点:他的房子,Hay-Adams酒店和白宫。在过去的几个月中,他还获得了3个冠军头衔:奥巴马参议员,当选总统奥巴马,很快将成为奥巴马总统。

如果您将日期“1/1/2008”传递到此查询中,您应该返回所有3个位置,其中“Senator Obama”(organization_name)属于他的“house”(organization_location)和其他两个地点使用我们的样本应该没有“组织”。如果你要通过“12/1/2008”的日期,他仍然在技术上住在他的家里,但他当时的头衔是“当选总统奥巴马”,所以结果会反映出来。如果你今天过日,他的名字仍然是“选举奥巴马总统”,但他的位置改为“Hay-Adams Hotel”。如果您在“2009年1月20日”之后的任何日期通过,他的头衔将是“奥巴马总统”,他的位置将是“白宫”。

我希望这是有道理的,即使你对政治没有兴趣或者不是来自美国......我需要结果向我展示一切都在哪里以及在给定的时间点所谓的一切。

答案 2 :(得分: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}}

感谢您花时间看看这个!