Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 通过具有_Linq_Linq To Sql - Fatal编程技术网

Linq 通过具有

Linq 通过具有,linq,linq-to-sql,Linq,Linq To Sql,有人能告诉我如何使用LINQtoSQL编写查询吗 搜索在特定州至少有一个场馆的郊区 位置 SuburbID SuburbName StateName 地点 编辑:最终答案: 如果您在地点/地点之间设置了外键: string stateName = "New York"; var locations = from loc in dataContext.Locations where loc.Venues.Count >

有人能告诉我如何使用LINQtoSQL编写查询吗 搜索在特定州至少有一个场馆的郊区

位置

SuburbID
SuburbName
StateName
地点

编辑:最终答案:

如果您在地点/地点之间设置了外键:

string stateName = "New York";  

var locations = from loc 
                in dataContext.Locations 
                where loc.Venues.Count > 1 && loc.StateName == stateName 
                select loc;

如果不存在外键关系:

string stateName = "New York";

var locations = (from v 
                 in dataContext.Venues 
                 where v.StateName == stateName 
                 select (from l 
                         in dataContext.Locations 
                         where l.SuburbName == v.SuburbName && l.StateName == v.Statename              
                         select l
                        ).Single()).Distinct();


不过,你真的应该把桌子修好。会议桌上应该有一个suburbID,而不是“StateName”和“suburbID Name”-保留这两个名称是多余的。

以下是几个简单的答案:

var suburbNames = dataContext.Venues
  .Where(v => v.StateName == specificState)
  .GroupBy(v => v.SuburbName)
  .Select(g => g.Key)

           //

var locations = dataContext.Location
  .Where(loc => loc.StateName == specificState)
  .Where(loc => loc.Venues.Any())

使用该Vinces属性:您可以通过在linq to sql designer中添加关系来实现这一点,即使外键在数据库中不存在/未强制执行。

我知道该表应该有SuburbID,但有时用户可能会输入SuburbID,但它还不在数据库中?如果它还不在数据库中,请在插入之前添加它场地。或者让用户先输入一个新的郊区,然后再向其中添加一个场馆。假设用户完全输入了一个场馆的信息-此时,您有足够的信息来存储郊区。(除非有我们看不到的字段)。
string stateName = "New York";

var locations = (from v 
                 in dataContext.Venues 
                 where v.StateName == stateName 
                 select (from l 
                         in dataContext.Locations 
                         where l.SuburbName == v.SuburbName && l.StateName == v.Statename              
                         select l
                        ).Single()).Distinct();
var suburbNames = dataContext.Venues
  .Where(v => v.StateName == specificState)
  .GroupBy(v => v.SuburbName)
  .Select(g => g.Key)

           //

var locations = dataContext.Location
  .Where(loc => loc.StateName == specificState)
  .Where(loc => loc.Venues.Any())