gpt4 book ai didi

linq - 构建动态 LINQ?

转载 作者:行者123 更新时间:2023-12-04 17:13:37 28 4
gpt4 key购买 nike

我正在使用 LINQ 来查询数据。考虑用户只想报告 3 个字段中的 1 个的情况? (见下文)

谁能告诉我如何动态构建查询?

谢谢

DD

var query =
from cl in db.tblClaims
join cs in db.tblCases
on cl.ref_no equals cs.ref_no
where cl.claim_status == "Appeal"
&& cl.appeal_date >= Convert.ToDateTime(txtReferedFromDate.Text)
&& cl.appeal_date <= Convert.ToDateTime(txtReferedToDate.Text)
&& cs.referred_from_lho == dlLHO.Text
&& cs.adviser == dlAdviser.Text
select new
{
Ref = cs.ref_no,
ClaimType = cl.claim_type,
ClaimStatus = cl.claim_status,
AppealDate = cl.appeal_date
};

gvReport.DataSource = query;

最佳答案

你可以这样做:

var query = from cl in db.tblClaims
join cs in db.tblCases
on cl.ref_no equals cs.ref_no
where cl.claim_status == "Appeal"
select new
{
Ref = cs.ref_no,
ClaimType = cl.claim_type,
ClaimStatus = cl.claim_status,
AppealDate = cl.appeal_date,
cs.referred_from_lho,
cs.adviser
};

if(!string.IsNullOrEmpty(txtReferedFromDate.Text)
&& !string.IsNullOrEmpty(txtReferedToDate.Text))
query = query.Where(cl =>
cl.appeal_date >= Convert.ToDateTime(txtReferedFromDate.Text)
&& cl.appeal_date <= Convert.ToDateTime(txtReferedToDate.Text));

if(!string.IsNullOrEmpty(dlLHO.Text))
query = query.Where(cl => cl.referred_from_lho == dlLHO.Text);

if(!string.IsNullOrEmpty(dlAdviser.Text))
query = query.Where(cl => cl.adviser == dlAdviser.Text);

gvReport.DataSource =
query.Select(o => new { o.Ref, o.ClaimType, o.ClaimStatus, o.AppealDate });

如果这些字段有要过滤的值,则只会将这些字段用作过滤器,否则它们将不会在查询中使用。

关于linq - 构建动态 LINQ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2493412/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com