C# 如何在列表中搜索LINQ SQL where值
我有一个名为Quotes的SQL表 在C中,我的用户:C# 如何在列表中搜索LINQ SQL where值,c#,.net,linq,C#,.net,Linq,我有一个名为Quotes的SQL表 在C中,我的用户: Public class Locations { public string cityname { get; set; } } List<Locations> = new puLocations List<Locations> // Pickup Locations List<Locations> = new delLocations List<Locations> // Deli
Public class Locations
{
public string cityname { get; set; }
}
List<Locations> = new puLocations List<Locations> // Pickup Locations
List<Locations> = new delLocations List<Locations> // Delivery Locations
现在我想用这些列表搜索报价表。。像这样的事情显然是行不通的
var quotes = from q in db.Quotes
where q.PULocations in puLocations //puLocatiosn is the list<Locations>
and q.DELLocations in delLocations
select q;
所以我想让它返回任何匹配项。。如果在普州,我有黄金海岸,悉尼,布里斯班。交货地点我有珀斯,霍巴特
它应该返回黄金海岸->珀斯
黄金海岸->霍巴特
悉尼->珀斯
悉尼->黄金海岸
.... 等等,如果这些引号存在,首先你需要这个
List<Locations> puLocations= new List<Locations>() // Pickup Locations
List<Locations> delLocations = new List<Locations>() // Delivery Locations
首先你需要这个
List<Locations> puLocations= new List<Locations>() // Pickup Locations
List<Locations> delLocations = new List<Locations>() // Delivery Locations
为了不让你的生活变得更艰难,你应该在db中进行这个操作,我打赌你的数据库中有收货和发货位置以及报价 因此,假设您有像db.Locations.Wherel=>l.type==DEL和db.Locations.Wherel=>l.type==PU这样的查询,您将编写以下查询
var quotes = from q in db.Quotes
join del in db.Locations on del equals q.DELLocations
join pu in db.Locations on pu equals q.PULocations
where pu.type == PU && del.type == DEL && otherConditions
select q;
此外,若数据库中确实存在约束,它们将作为导航属性传播,所以linq查询如下所示
var quotes = from q in db.Quotes where q.DELLocation.field == something && q.PULocation.field == something
但您可能没有约束,所以第一个查询将是最佳选择
最后一种方法是在语句中使用实体SQL,并编写如下所示的长语句
// you build string specifying as many parameters as you have items in your lists
var entitySQL = "select quote FROM [Quotes] WHERE quote.DELLocation.LocationID IN (@delparam1, @delparam2,.... ) AND quote.PULocation.LocationID in (@puparam1, @puparam2,.... )"
// then you create ObjectParameterCollection to substitute your IDs to ObjectQuery
var opc = new ObjectParameterCollection();
opc.Add("@delparam1", DELLocation[0].LocationID) // obviously you'll do it using linq
opc.Add("@puparam1", PULocation[0].LocationID)
// create object query
var quotes = db.CreateQuery<Quote>(entitySQL, opc);
为了不让你的生活变得更艰难,你应该在db中进行这个操作,我打赌你的数据库中有收货和发货位置以及报价 因此,假设您有像db.Locations.Wherel=>l.type==DEL和db.Locations.Wherel=>l.type==PU这样的查询,您将编写以下查询
var quotes = from q in db.Quotes
join del in db.Locations on del equals q.DELLocations
join pu in db.Locations on pu equals q.PULocations
where pu.type == PU && del.type == DEL && otherConditions
select q;
此外,若数据库中确实存在约束,它们将作为导航属性传播,所以linq查询如下所示
var quotes = from q in db.Quotes where q.DELLocation.field == something && q.PULocation.field == something
但您可能没有约束,所以第一个查询将是最佳选择
最后一种方法是在语句中使用实体SQL,并编写如下所示的长语句
// you build string specifying as many parameters as you have items in your lists
var entitySQL = "select quote FROM [Quotes] WHERE quote.DELLocation.LocationID IN (@delparam1, @delparam2,.... ) AND quote.PULocation.LocationID in (@puparam1, @puparam2,.... )"
// then you create ObjectParameterCollection to substitute your IDs to ObjectQuery
var opc = new ObjectParameterCollection();
opc.Add("@delparam1", DELLocation[0].LocationID) // obviously you'll do it using linq
opc.Add("@puparam1", PULocation[0].LocationID)
// create object query
var quotes = db.CreateQuery<Quote>(entitySQL, opc);
Contains不是LINQ扩展方法;它一直存在于长列表中。Contains不是LINQ扩展方法;它一直在那个里,就像那个长长的清单一样。