在以下场景中如何使用原始 Linq-to-SQL 对象添加 where 过滤器

发布于 2024-09-01 09:48:10 字数 2265 浏览 4 评论 0原文

我正在使用以下 Linq 表达式执行选择查询:

Table<Tbl_Movement> movements = context.Tbl_Movement;
var query = from m in movements
            select new MovementSummary
            {
                Id = m.DocketId,
                Created = m.DateTimeStamp,
                CreatedBy = m.Tbl_User.FullName,
                DocketNumber = m.DocketNumber,
                DocketTypeDescription = m.Ref_DocketType.DocketType,
                DocketTypeId = m.DocketTypeId,
                Site = new Site()
                {
                    Id = m.Tbl_Site.SiteId,
                    FirstLine = m.Tbl_Site.FirstLine,
                    Postcode = m.Tbl_Site.Postcode,
                    SiteName = m.Tbl_Site.SiteName,
                    TownCity = m.Tbl_Site.TownCity,
                    Brewery = new Brewery()
                    {
                        Id = m.Tbl_Site.Ref_Brewery.BreweryId,
                        BreweryName = m.Tbl_Site.Ref_Brewery.BreweryName
                    },
                    Region = new Region()
                    {
                        Description = m.Tbl_Site.Ref_Region.Description,
                        Id = m.Tbl_Site.Ref_Region.RegionId
                    }
                }
            };

我还将 IFilter 类传递到执行此选择的方法中。

public interface IJobFilter
{
    int? PersonId { get; set; }
    int? RegionId { get; set; }
    int? SiteId { get; set; }
    int? AssetId { get; set; }
}

如何将这些 where 参数添加到我的 SQL 表达式中?最好我希望用另一种方法完成此操作,因为过滤将在多个存储库中重复使用。

不幸的是,当我执行 query.Where 时,它已变成 IQueryable。我假设它已经变成这样,因为我返回了 IEnumerable。我才刚刚开始学习 LINQ,所以要温柔。

答案:

private IQueryable<Tbl_Docket> BuildQuery(IQueryable<Tbl_Docket> movements, IMovementFilter filter)
{
   if (filter != null)
   {
      if (filter.PersonId.HasValue) movements = movements.Where(m => m.UserId == filter.PersonId);
      if (filter.SiteId.HasValue) ...
   }
   return movements;
}

其调用方式如下:

var query = from m in this.BuildQuery(movements, filter)
            select new... {}

I am performing a select query using the following Linq expression:

Table<Tbl_Movement> movements = context.Tbl_Movement;
var query = from m in movements
            select new MovementSummary
            {
                Id = m.DocketId,
                Created = m.DateTimeStamp,
                CreatedBy = m.Tbl_User.FullName,
                DocketNumber = m.DocketNumber,
                DocketTypeDescription = m.Ref_DocketType.DocketType,
                DocketTypeId = m.DocketTypeId,
                Site = new Site()
                {
                    Id = m.Tbl_Site.SiteId,
                    FirstLine = m.Tbl_Site.FirstLine,
                    Postcode = m.Tbl_Site.Postcode,
                    SiteName = m.Tbl_Site.SiteName,
                    TownCity = m.Tbl_Site.TownCity,
                    Brewery = new Brewery()
                    {
                        Id = m.Tbl_Site.Ref_Brewery.BreweryId,
                        BreweryName = m.Tbl_Site.Ref_Brewery.BreweryName
                    },
                    Region = new Region()
                    {
                        Description = m.Tbl_Site.Ref_Region.Description,
                        Id = m.Tbl_Site.Ref_Region.RegionId
                    }
                }
            };

I am also passing in an IFilter class into the method where this select is performed.

public interface IJobFilter
{
    int? PersonId { get; set; }
    int? RegionId { get; set; }
    int? SiteId { get; set; }
    int? AssetId { get; set; }
}

How do I add these where parameters into my SQL expression? Preferably I'd like this done in another method as the filtering will be re-used across multiple repositories.

Unfortunately when I do query.Where it has become an IQueryable<MovementSummary>. I'm assuming it has become this as I'm returning an IEnumerable<MovementSummary>. I've only just started learning LINQ, so be gentle.

Answer:

private IQueryable<Tbl_Docket> BuildQuery(IQueryable<Tbl_Docket> movements, IMovementFilter filter)
{
   if (filter != null)
   {
      if (filter.PersonId.HasValue) movements = movements.Where(m => m.UserId == filter.PersonId);
      if (filter.SiteId.HasValue) ...
   }
   return movements;
}

Which is called like follows:

var query = from m in this.BuildQuery(movements, filter)
            select new... {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2024-09-08 09:48:10

您必须在触发 select 语句之前调用 where 语句,例如:

IQueryable<Tbl_Movement> movements = context.Tbl_Movement;
if (filter != null)
{
   if (filter.PersonId != null) movements = movements.Where(m => m....PersonId == filter.PersonId);
   if (filter.RegionId != null) movements = movements.Where(m => m....RegionId == filter.RegionId);
   if (filter.SiteId != null) movements = movements.Where(m => m...SiteId == filter.SiteId);
   if (filter.AssetId != null) movements = movements.Where(m => m...AssetId == filter.AssetId);
}

 var query = m from movements...

与使用此 IFilter 类相反,您可能需要考虑 Fluent Pipe-based Repository 结构,例如:

var movements = new MovementsPipe()
    .FindSiteId(1)
    .FindAssetIds(1, 2, 3)
    .FindRegionId(m => m > 10)
    .ToMovementSummaryList();

希望这有帮助。如果您有任何疑问,请告诉我。

You have to call the where statement before you fire your select statement, e.g.:

IQueryable<Tbl_Movement> movements = context.Tbl_Movement;
if (filter != null)
{
   if (filter.PersonId != null) movements = movements.Where(m => m....PersonId == filter.PersonId);
   if (filter.RegionId != null) movements = movements.Where(m => m....RegionId == filter.RegionId);
   if (filter.SiteId != null) movements = movements.Where(m => m...SiteId == filter.SiteId);
   if (filter.AssetId != null) movements = movements.Where(m => m...AssetId == filter.AssetId);
}

 var query = m from movements...

As opposed to using this IFilter class, you might want to consider a Fluent Pipe-based Repository structure, e.g.:

var movements = new MovementsPipe()
    .FindSiteId(1)
    .FindAssetIds(1, 2, 3)
    .FindRegionId(m => m > 10)
    .ToMovementSummaryList();

Hope this helps. Let me know if you have any questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文