如何使用 linq 查询多个?
How to QueryMultiple using linq?
如何使用 LINQ select 喜欢下面的代码,或者我可以在 LINQ 上使用 QueryMultiple 吗?。我知道如何使用 LINQ 加入两个模型列表,但在这种情况下,我不知道如何使用 LINQ 执行此操作。
var result = con.QueryMultiple(@"select gtp.status, gtp.p_id, gtp.p_name, gtp.p_price,
gca.c_type, gbr.bedtype, gtp.currency from t_package as gtp, category as gca,
bedrooms as gbr where gtp.c_id = gca.c_id and gtp.bedroom = gbr.bed_id
and gtp.p_id = @id;
select gtd.tdates_id, gtd.dfrom, gtd.dto from traveldates as gtd
where gtd.p_id = @id;
select ginc.in_id, ginc.name from inclusion as ginc
where ginc.p_id = @id;
select con.con_id, con.name from tcondition as con
where con.p_id = @id;
select exc.ex_id, exc.name from exclusion as exc
where exc.p_id = @id;
select fli.fl_id, fli.name from flightdetails as fli
where fli.p_id = @id;
select iti.it_id, iti.name, iti.description, iti.bmeal, iti.lmeal, iti.dmeal from itinerary as iti
where iti.p_id = @id;
select img.img_id, img.slides from imagetable as img
where img.p_id = @id;
select vi.visa_id, vi.name from visareq as vi
where vi.p_id = @id;
select ph.ph_id, ph.ph_loc, ph.ph_phtel from photel as ph
where ph.p_id = @id;",
new { @id = id });
var aTp = result.ReadSingle<AdminTP>();
aTp.adminTDs = result.Read<AdminTD>().ToList();
aTp.adminINCs = result.Read<AdminINC>().ToList();
aTp.adminCONs = result.Read<AdminCON>().ToList();
aTp.adminEXCs = result.Read<AdminEXC>().ToList();
aTp.adminFLIs = result.Read<AdminFLI>().ToList();
aTp.adminITIs = result.Read<AdminITI>().ToList();
aTp.adminIMGs = result.Read<AdminIMG>().ToList();
aTp.adminVIs = result.Read<AdminVI>().ToList();
aTp.photels = result.Read<Photel>().ToList();
return Json(aTp, JsonRequestBehavior.AllowGet);
假设 t_package、traveldates 和其他 table 是模型列表而不是数据库 table。
所以您有 Packages、TravelDates、Gincs、Cons、Flis 等序列,而且 Packages 和这些其他项目之间似乎存在一对多关系。
每个Package都有零个或多个TDates,每个TDates恰好属于一个Package,即外键PackageId所指的Package。
同理,每个Ginc都属于外键所指的Package,每个Con、Fli等都属于外键PackageId所指的唯一一个包:都是直接的一对多关系
Requirement: given a packageId, give me the Package with this Id, with its travel data, its Gincs, its Flis, and all its other items with strange names.
为了能够使用 LINQ 执行此操作,您需要有权访问 IQueryables。例如,通过 DbContext.DbSet<...>
int packageId = ...
IQueryable<Package> packages = ...
IQueryable<TDate> tDates = ...
IQueryable<Fli> Flis = ...
...
var result = packages.Where(package => package.Id == packageId).Select(package => new
{
// Select the package properties that you plan to use:
Id = package.Id,
Name = package.Name,
Status = package.Status,
...
TDates = tDates.Where(tDate => tDate.PackageId == package.Id).Select(tDate => new
{
Id = tDate.Id,
DateFrom = tDate.DateFrom,
DateTo = tDate.DateTo,
...
})
.ToList(),
Flis = flis.Where(fli => fli.PackageId == package.Id).Select(fli => new
{
...
})
.ToList(),
... etc:
});
我想你正在寻找联合命令,你可以使用Linq的查询符号或点符号。在进行联合之前,您需要执行您在评论中提到的 select ,以便附加的每个对象都属于同一类型。下面的代码是点符号,因为我觉得它更容易阅读:
string[] adminTDs = { "A", "B", "C" };
string[] adminINCs= { "D", "E", "F" };
string[] adminCONs = { "H", "I", "J" };
//Method Syntax
var MS = adminTDs
.Union(adminINCs)
.Union(adminCONs).ToList();
有关 LINQ 联合的更多信息,请访问 https://dotnettutorials.net/lesson/linq-union-method/。
如何使用 LINQ select 喜欢下面的代码,或者我可以在 LINQ 上使用 QueryMultiple 吗?。我知道如何使用 LINQ 加入两个模型列表,但在这种情况下,我不知道如何使用 LINQ 执行此操作。
var result = con.QueryMultiple(@"select gtp.status, gtp.p_id, gtp.p_name, gtp.p_price,
gca.c_type, gbr.bedtype, gtp.currency from t_package as gtp, category as gca,
bedrooms as gbr where gtp.c_id = gca.c_id and gtp.bedroom = gbr.bed_id
and gtp.p_id = @id;
select gtd.tdates_id, gtd.dfrom, gtd.dto from traveldates as gtd
where gtd.p_id = @id;
select ginc.in_id, ginc.name from inclusion as ginc
where ginc.p_id = @id;
select con.con_id, con.name from tcondition as con
where con.p_id = @id;
select exc.ex_id, exc.name from exclusion as exc
where exc.p_id = @id;
select fli.fl_id, fli.name from flightdetails as fli
where fli.p_id = @id;
select iti.it_id, iti.name, iti.description, iti.bmeal, iti.lmeal, iti.dmeal from itinerary as iti
where iti.p_id = @id;
select img.img_id, img.slides from imagetable as img
where img.p_id = @id;
select vi.visa_id, vi.name from visareq as vi
where vi.p_id = @id;
select ph.ph_id, ph.ph_loc, ph.ph_phtel from photel as ph
where ph.p_id = @id;",
new { @id = id });
var aTp = result.ReadSingle<AdminTP>();
aTp.adminTDs = result.Read<AdminTD>().ToList();
aTp.adminINCs = result.Read<AdminINC>().ToList();
aTp.adminCONs = result.Read<AdminCON>().ToList();
aTp.adminEXCs = result.Read<AdminEXC>().ToList();
aTp.adminFLIs = result.Read<AdminFLI>().ToList();
aTp.adminITIs = result.Read<AdminITI>().ToList();
aTp.adminIMGs = result.Read<AdminIMG>().ToList();
aTp.adminVIs = result.Read<AdminVI>().ToList();
aTp.photels = result.Read<Photel>().ToList();
return Json(aTp, JsonRequestBehavior.AllowGet);
假设 t_package、traveldates 和其他 table 是模型列表而不是数据库 table。
所以您有 Packages、TravelDates、Gincs、Cons、Flis 等序列,而且 Packages 和这些其他项目之间似乎存在一对多关系。
每个Package都有零个或多个TDates,每个TDates恰好属于一个Package,即外键PackageId所指的Package。
同理,每个Ginc都属于外键所指的Package,每个Con、Fli等都属于外键PackageId所指的唯一一个包:都是直接的一对多关系
Requirement: given a packageId, give me the Package with this Id, with its travel data, its Gincs, its Flis, and all its other items with strange names.
为了能够使用 LINQ 执行此操作,您需要有权访问 IQueryables。例如,通过 DbContext.DbSet<...>
int packageId = ...
IQueryable<Package> packages = ...
IQueryable<TDate> tDates = ...
IQueryable<Fli> Flis = ...
...
var result = packages.Where(package => package.Id == packageId).Select(package => new
{
// Select the package properties that you plan to use:
Id = package.Id,
Name = package.Name,
Status = package.Status,
...
TDates = tDates.Where(tDate => tDate.PackageId == package.Id).Select(tDate => new
{
Id = tDate.Id,
DateFrom = tDate.DateFrom,
DateTo = tDate.DateTo,
...
})
.ToList(),
Flis = flis.Where(fli => fli.PackageId == package.Id).Select(fli => new
{
...
})
.ToList(),
... etc:
});
我想你正在寻找联合命令,你可以使用Linq的查询符号或点符号。在进行联合之前,您需要执行您在评论中提到的 select ,以便附加的每个对象都属于同一类型。下面的代码是点符号,因为我觉得它更容易阅读:
string[] adminTDs = { "A", "B", "C" };
string[] adminINCs= { "D", "E", "F" };
string[] adminCONs = { "H", "I", "J" };
//Method Syntax
var MS = adminTDs
.Union(adminINCs)
.Union(adminCONs).ToList();
有关 LINQ 联合的更多信息,请访问 https://dotnettutorials.net/lesson/linq-union-method/。