linq查询发出多个表

时间:2017-03-31 04:44:31

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

这是我有男人/女孩...我有3个表如下(只包括重要的列):

ITEMS:

ItemId  Name            ItemLocationId
20      Portal Orange   12  
21      Portal Blue     13

ITEMLOCATIONS:

ItemLocationid  ItemId  CreateDate              LocationIf
13              21      3/26/2017 2:19:15 AM    5
14              20      3/27/2017 6:25:45 PM    6
15              21      3/31/2017 12:17:25 AM   6
16              21      3/31/2017 12:18:42 AM   5
17              21      3/31/2017 12:20:23 AM   6

LOCATIONS

LocationId  Name
5           Storage Room
6           Boss Room

我的问题在于itemlocations表...我只需要表中最新的ItemLocation ......其他的是历史价值......这是我现在正在运行的查询

Dim i = From r In mydb.Items
        Join il In mydb.ItemLocations On r.ItemLocationId Equals il.ItemLocationId
        Join l In mydb.Locations On il.LocationId Equals l.LocationId
        Where r.CompanyId = UserPro.CompanyId
        Select r.ItemId, r.Name, Location = l.Name

这是返回表中该项目的第一个itemlocation ...我如何只得到那个最近的那个

2 个答案:

答案 0 :(得分:1)

这个怎么样

Dim groupQuery=from il mydb.ItemLocations
  .group il by il.ItemId into gl
               select gl.orderByDescending(g=>g.CreateDate).First();


Dim i = From r In mydb.Items
        Join il In groupQuery On r.ItemLocationId Equals il.ItemLocationId
        Join l In mydb.Locations On il.LocationId Equals l.LocationId
        Where r.CompanyId = UserPro.CompanyId
        Select r.ItemId, r.Name, Location = l.Name

答案 1 :(得分:1)

没有可用的数据库,因此在LinqPad中为您绘制了答案。 您需要按日期选择:

void Main()
{
List<Item> Items = new List<Item> { new Item { ItemID=20, ItemLocationID=12, Name="Portal Orqange"},
new Item{ ItemID=21, ItemLocationID=13, Name="Portal Blue"}};

List<ItemLocation> ItemLocations = new List<ItemLocation> { 
                                    new ItemLocation {ItemLocationID=13,  ItemId=21,  CreateDate=DateTime.Parse("3/26/2017 2:19:15 AM"),  LocationId=5},
                                    new ItemLocation {ItemLocationID=14,  ItemId=20,  CreateDate=DateTime.Parse("3/27/2017 6:25:45 PM"),  LocationId=6},
                                    new ItemLocation {ItemLocationID=15,  ItemId=21,  CreateDate=DateTime.Parse("3/31/2017 12:17:25 AM"),  LocationId=6},
                                    new ItemLocation {ItemLocationID=16,  ItemId=21,  CreateDate=DateTime.Parse("3/31/2017 12:18:42 AM"),  LocationId=5},
                                    new ItemLocation {ItemLocationID=17,  ItemId=21,  CreateDate=DateTime.Parse("3/31/2017 12:20:23 AM"),  LocationId=6},
                                    };
List<Location> Locations = new List<Location> { new Location { LocationID=5, Name="Storage Room"},
                                                new Location { LocationID=6, Name="Boss Room"}
                                                };

Items.Join(ItemLocations, i => i.ItemID, il => il.ItemId, (i, il) => new { i, il }).OrderByDescending(i =>i.il.CreateDate )
    .Join(Locations, iil => iil.il.LocationId, il=>il.LocationID, (lc, c) => new {lc,c}).FirstOrDefault()
    .Dump();


}

// Define other methods and classes here
public class Item
{
public int ItemID { get; set; }
public string Name { get; set; }
public int ItemLocationID { get; set;}

}

public class ItemLocation
{
public int ItemLocationID { get; set; }
public int ItemId { get; set; }
public DateTime CreateDate { get; set; }
public int LocationId { get; set;}
}

public class Location
{
public int LocationID { get; set; }
public string Name { get; set;}
}

准备好编码时,只需将LinqPad特定的Dump()替换为Select。 结果如下: enter image description here