是否可以同时执行两个不同类型的 LINQ 查询?
Is it Possible to execute two LINQ queries of different types at the same time?
我有两个模型 Material 请求和安全用户:
public class MaterialRequestMaster : BaseDomain<long>
{
public string RequestCode { get; private set; }
public string Description { get; private set; }
public GlobalEnums.MaterialRequestStatus Status { get; set; }
public int UserId { get; private set; }
public SecurityUser User { get; private set; }
}
public class SecurityUser :IdentityUser<int>
{
public string Name { get; set; }
public int PersonelCodeId { get; set; }
public ICollection<MaterialRequestMaster> MaterialRequestMaster { get; set; }
}
我想根据用户名搜索 Material 请求,所以我制作了一个 Material 请求的视图模型,如下所示:
public class MaterialRequestMasterViewModel
{
public long Id { get; set; }
[Required(ErrorMessage = "Please Enter The Code")]
public string RequestCode { get; set; }
public GlobalEnums.MaterialRequestStatus Status{ get; set; }
[MaxLength(255)]
public string Description { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationDate { get; set; }
}
我从视图中获取用户名并创建一个类似“x=>x.UserName.Contains("Something")" 的字符串并将其发送到我的 Material 请求搜索方法作为 searchQuery:
public IPagedList<MaterialRequestMasterViewModel> GetPagedRecords(int pageNumber, int pageSize, string orderByCol, string order, string searchQuery)
{
IQueryable<MaterialRequestMaster> query;
query = query = _IMRMRepository.Get(); //Gets all records of <MaterialRecustMaster>
IQueryable<MaterialRequestMasterViewModel> searchedQuery;
searchedQuery = query.Select(n => new MaterialRequestMasterViewModel //Fill ViewModel
(
n.Id,
n.RequestCode,
n.Status,
n.Description,
n.UserId,
n.User.Name,
n.CreationDate,
n.IsDeleted
));
if (!string.IsNullOrWhiteSpace(searchQuery))
searchedQuery = searchedQuery.Where(searchQuery);
return searchedQuery.ToPagedList(pageNumber, pageSize);
}
由于 LINQ 转换问题,ToPagedList() 方法无法执行查询。
这是正确的做法吗?
where 的参数必须是布尔函数,所以你应该这样发送而不是字符串。
public IPagedList<MaterialRequestMasterViewModel> GetPagedRecords(int pageNumber, int pageSize, string orderByCol, string order, Func<SecurityUser ,Boolean> searchQuery )
{
IQueryable<MaterialRequestMaster> query;
query = query = _IMRMRepository.Get(); //Gets all records of <MaterialRecustMaster>
IQueryable<MaterialRequestMasterViewModel> searchedQuery;
searchedQuery = query.Select(n => new MaterialRequestMasterViewModel //Fill ViewModel
(
n.Id,
n.RequestCode,
n.Status,
n.Description,
n.UserId,
n.User.Name,
n.CreationDate,
n.IsDeleted
));
if (searchQuery != null)
searchedQuery = searchedQuery.Where(searchQuery);
return searchedQuery.ToPagedList(pageNumber, pageSize);
}
您可以像这样创建函数变量
Func<SecurityUser, boolean> searchFunc = x => x.UserName.Contains("Something");
您可以像这样组合多个此类函数
Func<SecurityUser, boolean> combined = x=> searchFunc(x) || x.Id > 100;
等等
如果您将“SearchQuery”作为字符串格式传递给 GetPagedRecords() 方法,您可以在“MaterialRequestMasterViewModel”视图模型中的 属性 用户 ID 的 header 添加一个属性安全用户模型的名称。
public class MaterialRequestMasterViewModel
{
public long Id { get; set; }
[Required(ErrorMessage = "Please Enter The Code")]
public string RequestCode { get; set; }
public GlobalEnums.MaterialRequestStatus Status{ get; set; }
[MaxLength(255)]
public string Description { get; set; }
[RelationName="SecurityUser"]
public int UserId { get; set; }
public string UserName { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationDate { get; set; }
}
然后使用反射获取 属性 属性的值并将其添加到您的 where 子句中。
搜索查询值应该是这样的:
RelationPropertyValue = assembly.GetProperty("UserId").GetCustomAttribute<RelationName>();
searchQuery = $"x => x.{RelationPropertyValue}.Contains(\"Something\")"
我认为它对你有用。
我有两个模型 Material 请求和安全用户:
public class MaterialRequestMaster : BaseDomain<long>
{
public string RequestCode { get; private set; }
public string Description { get; private set; }
public GlobalEnums.MaterialRequestStatus Status { get; set; }
public int UserId { get; private set; }
public SecurityUser User { get; private set; }
}
public class SecurityUser :IdentityUser<int>
{
public string Name { get; set; }
public int PersonelCodeId { get; set; }
public ICollection<MaterialRequestMaster> MaterialRequestMaster { get; set; }
}
我想根据用户名搜索 Material 请求,所以我制作了一个 Material 请求的视图模型,如下所示:
public class MaterialRequestMasterViewModel
{
public long Id { get; set; }
[Required(ErrorMessage = "Please Enter The Code")]
public string RequestCode { get; set; }
public GlobalEnums.MaterialRequestStatus Status{ get; set; }
[MaxLength(255)]
public string Description { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationDate { get; set; }
}
我从视图中获取用户名并创建一个类似“x=>x.UserName.Contains("Something")" 的字符串并将其发送到我的 Material 请求搜索方法作为 searchQuery:
public IPagedList<MaterialRequestMasterViewModel> GetPagedRecords(int pageNumber, int pageSize, string orderByCol, string order, string searchQuery)
{
IQueryable<MaterialRequestMaster> query;
query = query = _IMRMRepository.Get(); //Gets all records of <MaterialRecustMaster>
IQueryable<MaterialRequestMasterViewModel> searchedQuery;
searchedQuery = query.Select(n => new MaterialRequestMasterViewModel //Fill ViewModel
(
n.Id,
n.RequestCode,
n.Status,
n.Description,
n.UserId,
n.User.Name,
n.CreationDate,
n.IsDeleted
));
if (!string.IsNullOrWhiteSpace(searchQuery))
searchedQuery = searchedQuery.Where(searchQuery);
return searchedQuery.ToPagedList(pageNumber, pageSize);
}
由于 LINQ 转换问题,ToPagedList() 方法无法执行查询。 这是正确的做法吗?
where 的参数必须是布尔函数,所以你应该这样发送而不是字符串。
public IPagedList<MaterialRequestMasterViewModel> GetPagedRecords(int pageNumber, int pageSize, string orderByCol, string order, Func<SecurityUser ,Boolean> searchQuery )
{
IQueryable<MaterialRequestMaster> query;
query = query = _IMRMRepository.Get(); //Gets all records of <MaterialRecustMaster>
IQueryable<MaterialRequestMasterViewModel> searchedQuery;
searchedQuery = query.Select(n => new MaterialRequestMasterViewModel //Fill ViewModel
(
n.Id,
n.RequestCode,
n.Status,
n.Description,
n.UserId,
n.User.Name,
n.CreationDate,
n.IsDeleted
));
if (searchQuery != null)
searchedQuery = searchedQuery.Where(searchQuery);
return searchedQuery.ToPagedList(pageNumber, pageSize);
}
您可以像这样创建函数变量
Func<SecurityUser, boolean> searchFunc = x => x.UserName.Contains("Something");
您可以像这样组合多个此类函数
Func<SecurityUser, boolean> combined = x=> searchFunc(x) || x.Id > 100;
等等
如果您将“SearchQuery”作为字符串格式传递给 GetPagedRecords() 方法,您可以在“MaterialRequestMasterViewModel”视图模型中的 属性 用户 ID 的 header 添加一个属性安全用户模型的名称。
public class MaterialRequestMasterViewModel
{
public long Id { get; set; }
[Required(ErrorMessage = "Please Enter The Code")]
public string RequestCode { get; set; }
public GlobalEnums.MaterialRequestStatus Status{ get; set; }
[MaxLength(255)]
public string Description { get; set; }
[RelationName="SecurityUser"]
public int UserId { get; set; }
public string UserName { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationDate { get; set; }
}
然后使用反射获取 属性 属性的值并将其添加到您的 where 子句中。 搜索查询值应该是这样的:
RelationPropertyValue = assembly.GetProperty("UserId").GetCustomAttribute<RelationName>();
searchQuery = $"x => x.{RelationPropertyValue}.Contains(\"Something\")"
我认为它对你有用。