Linq @ Where子句具有多个条件

时间:2020-09-11 10:13:58

标签: linq

这是我的导出用户界面

enter image description here

这是我的代码:

Exception thrown at 0x00007FF7AB961478 in Sandbox.exe: 0xC0000005: Access violation writing location 0x00007FF7AB969C58

问题是,如果我仅在dropdownlist:family中选择区域,它将生成正确的列表。 但是当我选择dropdownlist:family&status时,它什么也不会产生。

这是表格:

enter image description here

1 个答案:

答案 0 :(得分:0)

我将更改您的代码以检查每个过滤条件并添加适当的过滤器,然后转换为列表:

var wrq = db.Tbl_WorkRequest.AsQueryable();
if (wrstatus.Length > 0)
    wrq = wrq.Where(wr => wr.WR_Status == wrstatus);
if (wrfamily.Length > 0)
    wrq = wrq.Where(wr => wr.WR_Family == wrfamily);

var wrlist = wrq.Select(d => new {
                    WR_Title = d.WR_Title.ToString(),
                    WR_Type = d.WR_Type.ToString(),
                    WR_Family = d.WR_Family.ToString(),
                    WR_Status = d.WR_Status.ToString(),
                    WR_LocationAsset = d.WR_LocationAsset.ToString(),
                    WR_AssetName = d.WR_AssetName.ToString(),
                    WR_Requestor = d.WR_Requestor.ToString()
                })
                .ToList();