How do I add a where filter using the original Linq-to-SQL object in the following scenario

Posted by GenericTypeTea on Stack Overflow See other posts from Stack Overflow or by GenericTypeTea
Published on 2010-05-09T20:14:10Z Indexed on 2010/05/09 20:18 UTC
Read the original article Hit count: 193

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about LINQ