我试图构建一个asp.net webforms应用程序作为sql数据库的前端。
see database table relations in this image
简单地获取表值不是问题。 例如,显示来自'项目'像这样的表:
var items = from i in db.Items
select new
{
ID = i.Item_ID,
Name = i.Name,
Barcode = i.BarCode,
Description = i.Description,
ItemType = i.ItemType1.ItemTypeName,
LocationCount = i.Location_Item_Juncs.Count
};
GridView1.DataSource = items;
GridView1.DataBind();
looks like this in webforms webpage
问题是获得项目的供应商信息。
一个项目可以有多个“供应商”,“位置”和“#39;和' ReceivedDate'!
在SQL中,我可以查询这样的信息:
select Supplier.Name, Supplier.Adress, Supplier.Email, Supplier.Phone, Supplier.Supplier_Zipcode
from item, Supp_Company, Supplier
where Item_ID = 8 and Item_ID = ItemSub_ID and SupplierJunc_ID = Supplier_ID
results look like this in linqpad
这些是供应商'商品ID为8的商品的信息。
请注意,查询中涉及3个表(Item,Supp_Company,Supplier),并且必须匹配2对值才能选择有效值。
我想在LINQ中复制该查询以在我的Web表单应用程序中使用。 我相信这个问题的解决方案将适用于获取地点和“收到日期”#39;对于一个项目也是如此。
是否可以使用类似的'其中' LINQ中的子句我可以在SQL中使用吗?语法是什么样的?
答案 0 :(得分:0)
链接表方法(请注意modelBuilder HasMany WithMany
)
void Main()
{
using (var context = new YourContext())
{
var query = from item in context.Items
from supplier in item.Suppliers
where item.ItemId == 8
select new
{
Name = supplier.Name,
Adress = supplier.Address,
Email = supplier.Email,
Phone = supplier.Phone,
Zip = supplier.Zip,
};
//...
}
}
public class YourContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public YourContext() : base("MyDb")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
.HasMany(item => item.Suppliers)
.WithMany(supplier => supplier.Items)
.Map(m =>
{
m.MapLeftKey("ItemSub_ID");
m.MapRightKey("SupplierJunc_ID");
m.ToTable("Supp_Company");
});
}
}
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public ICollection<Supplier> Suppliers { get; set; }
}
public class Supplier
{
public int SupplierId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Zip { get; set; }
public ICollection<Item> Items { get; set; }
}
链接实体方法(请注意modelBuilder
将每个表映射到实体)
void Main()
{
using (var context = new YourContext())
{
var query = from item in context.Items
join link in context.SupplierItems
on item.ItemId equals link.ItemId
join supplier in context.Suppliers
on link.SupplierId equals supplier.SupplierId
where item.ItemId == 8
select new
{
Name = supplier.Name,
Adress = supplier.Address,
Email = supplier.Email,
Phone = supplier.Phone,
Zip = supplier.Zip,
};
//...
}
}
public class YourContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<SupplierItem> SupplierItems { get; set; }
public YourContext() : base("MyDb")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
// ...
;
modelBuilder.Entity<Supplier>()
// ...
;
modelBuilder.Entity<SupplierItem>()
// ...
;
}
}
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
}
public class Supplier
{
public int SupplierId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Zip { get; set; }
}
public class SupplierItem
{
public int ItemId { get; set; }
public int SupplierId { get; set; }
}