C# 如何在linq中添加条件联接?
我想根据我传递的参数在linq中添加2个不同的连接。 假设isHub是参数。 如果isHub=true:我想返回城市列表 如果isHub=false:我想返回国家列表 这是我目前的问题C# 如何在linq中添加条件联接?,c#,linq,api,C#,Linq,Api,我想根据我传递的参数在linq中添加2个不同的连接。 假设isHub是参数。 如果isHub=true:我想返回城市列表 如果isHub=false:我想返回国家列表 这是我目前的问题 public List<ControlTowerCity> GetControlTowerMapData(bool IsHub) { using (var context = new LadingControlTowerEntities()) {
public List<ControlTowerCity> GetControlTowerMapData(bool IsHub)
{
using (var context = new LadingControlTowerEntities())
{
var mapcityDetail =
(from SLD in context.ShipmentLocations
join CMD in context.CityMasters on SLD.City equals CMD.ID
select new ControlTowerCity
{
Name = CMD.Name,
}).ToList();
return mapcityDetail;
}
}
感谢您的帮助。对于此任务,使用将iQueryTables连接起来比使用查询语法更容易。例如,请参阅javad amiry的答案,以避免额外的from和select语句的开销 但是为了回答您的问题:使用查询语法,您需要将IQueryable存储在一个额外的变量中,并在“从上到下”联接的in部分中再次使用它
var query = (
from SLD in Context.ShipmentLocations
where ... // you could add some filters here
select SLD
);
IQueryable<ControlTowerCity> query2;
if(ishHub == true)
{
query2 = (
from SLD in query
join CMD in context.CityMasters on SLD.City equals CMD.ID
select CMD
)
}
else {
query2 = (
from SLD in query
join CMD in context.CountryMasters on SLD.Country equals CMD.ID
select CMD
)
}
var result = (
from CMD in query2
select new ControlTowerCity
{
Name = CMD.Name,
}
).ToList();
对于此任务,使用来连接IQueryables比使用查询语法更容易,例如,请参见javad amiry的答案,以避免额外的from和select语句的开销 但是为了回答您的问题:使用查询语法,您需要将IQueryable存储在一个额外的变量中,并在“从上到下”联接的in部分中再次使用它
var query = (
from SLD in Context.ShipmentLocations
where ... // you could add some filters here
select SLD
);
IQueryable<ControlTowerCity> query2;
if(ishHub == true)
{
query2 = (
from SLD in query
join CMD in context.CityMasters on SLD.City equals CMD.ID
select CMD
)
}
else {
query2 = (
from SLD in query
join CMD in context.CountryMasters on SLD.Country equals CMD.ID
select CMD
)
}
var result = (
from CMD in query2
select new ControlTowerCity
{
Name = CMD.Name,
}
).ToList();
你为什么不能有一个if..else这样的分支
public List<ControlTowerCity> GetControlTowerMapData(bool IsHub)
{
if(isHub)
{
using (var context = new LadingControlTowerEntities())
{
var mapcityDetail =
(from SLD in context.ShipmentLocations
join CMD in context.CityMasters on SLD.City equals CMD.ID
select new ControlTowerCity
{
Name = CMD.Name,
}).ToList();
return mapcityDetail;
}
}
else
{
// other part of query with different join
}
}
你为什么不能有一个if..else这样的分支
public List<ControlTowerCity> GetControlTowerMapData(bool IsHub)
{
if(isHub)
{
using (var context = new LadingControlTowerEntities())
{
var mapcityDetail =
(from SLD in context.ShipmentLocations
join CMD in context.CityMasters on SLD.City equals CMD.ID
select new ControlTowerCity
{
Name = CMD.Name,
}).ToList();
return mapcityDetail;
}
}
else
{
// other part of query with different join
}
}
我建议创建IQueryable的通用扩展函数。通过这种方式,您可以将其用于每个必须根据某个输入变量选择A或B的问题 看 我将创建的扩展函数类似于标准 不同之处在于它有一个额外的输入布尔参数,而不是一个outerKeySelector,它有两个outerKeySelectorTrue和outerKeySelectorFalse 如果布尔参数为true,我们将使用outerKeySelectorTrue进行连接,否则将使用outerkeySelectorFalse进行连接 扩展联接 我们有两个outerKeySelector,而不是一个outerKeySelector,其余的相当于一个标准联接
bool isHub = ...
var result = dbContext.ShipmentLocations // join ShipmentLocations
.Join(dbContext.CityMasters, // with CityMasters
// the boolean that decides which of the two outerKeySelectors to use
isHub,
// the two outerKeySelectors: one for isHub true and one for isHub fale
shipmentLocation => shipmentLocation.City,
shipmentLocation => shipmentLocation.Country,
// the innerKeySelector: used to compare with the selected outerKey
cityMaster => cityMaster.Id,
(shipmentLocation, cityMaster) => new // when they match, make one new object
{
// select only the properties you plan to use
...
});
好的是,这适用于所有iQueryTables,无论它们是发货地点和城市管理员,还是有学生的学校、有订单的客户等等
此外,它与LINQ的使用方式非常相似,我建议创建IQueryable的通用扩展函数,这只是一条语句。通过这种方式,您可以将其用于每个必须根据某个输入变量选择A或B的问题 看 我将创建的扩展函数类似于标准 不同之处在于它有一个额外的输入布尔参数,而不是一个outerKeySelector,它有两个outerKeySelectorTrue和outerKeySelectorFalse 如果布尔参数为true,我们将使用outerKeySelectorTrue进行连接,否则将使用outerkeySelectorFalse进行连接 扩展联接 我们有两个outerKeySelector,而不是一个outerKeySelector,其余的相当于一个标准联接
bool isHub = ...
var result = dbContext.ShipmentLocations // join ShipmentLocations
.Join(dbContext.CityMasters, // with CityMasters
// the boolean that decides which of the two outerKeySelectors to use
isHub,
// the two outerKeySelectors: one for isHub true and one for isHub fale
shipmentLocation => shipmentLocation.City,
shipmentLocation => shipmentLocation.Country,
// the innerKeySelector: used to compare with the selected outerKey
cityMaster => cityMaster.Id,
(shipmentLocation, cityMaster) => new // when they match, make one new object
{
// select only the properties you plan to use
...
});
好的是,这适用于所有iQueryTables,无论它们是发货地点和城市管理员,还是有学生的学校、有订单的客户等等
此外,它与LINQ的使用方式非常相似,而且它只有一条语句可能在iSHUB中加入CMD?context.CityMasters:SLD.City上的context.CountryMasters等于CMD.ID,但我不确定它是否会编译。你能试着告诉我吗?@Rafalon我试过这个,不,是的,我期待着这个。我认为只有当context.CityMasters与context.countrymasters的类型相同时,它才能工作。是否可以在ishub中加入CMD?context.CityMasters:SLD.City上的context.CountryMasters等于CMD.ID,但我不确定它是否会编译。你能试着告诉我吗?@Rafalon我试过这个,不,是的,我期待着这个。我认为只有当context.CityMasters与context.CountryMastersthanks@javad amiry的类型相同时,它才能工作,如何在这个查询中应用过滤器?在这种情况下,我需要应用where条件,其中shipmentlocations.Status=a我在您的答案中所做的类似于var query=context.shipmentlocations.AsQueryable。其中x=>x.Status==A;是吗?@ChetanBirajdar是的,你做得对。我将编辑答案并添加条件SampleThank@javad amiry,如何在此查询中应用过滤器?在这种情况下,我需要应用where条件,其中shipmentlocations.Status=a我在您的答案中所做的类似于var query=context.shipmentlocations.AsQueryable。其中x=>x.Status==A;是吗?@ChetanBirajdar是的,你做得对。我将编辑答案并添加条件示例
bool isHub = ...
var result = dbContext.ShipmentLocations // join ShipmentLocations
.Join(dbContext.CityMasters, // with CityMasters
// the boolean that decides which of the two outerKeySelectors to use
isHub,
// the two outerKeySelectors: one for isHub true and one for isHub fale
shipmentLocation => shipmentLocation.City,
shipmentLocation => shipmentLocation.Country,
// the innerKeySelector: used to compare with the selected outerKey
cityMaster => cityMaster.Id,
(shipmentLocation, cityMaster) => new // when they match, make one new object
{
// select only the properties you plan to use
...
});