views:

27

answers:

3

I'm creating a repository and service layer in my app, and my repo has a very simple function

    Public Function GetRegions() As IQueryable(Of Region) Implements IRegionRepository.GetRegions
        Dim region = (From r In dc.Regions
                 Select r)
        Return region.AsQueryable
    End Function

Now in my Service layer I've got a function like this

    Public Function GetRegionById(ByVal id As Integer) As Region Implements IRegionService.GetRegionById
        Return _RegionRepository.GetRegions().Where(Function(r) r.ID = id).FirstOrDefault
    End Function

But i can't figure out how to add And r.isActive = True

Can anyone point me in the right direction on how to have multiple operators in this query?

+1  A: 

Not a vb guy as much as c# but the expression used in the where statement should be where it is appended. So you currently have r.ID = id. I would just add it there. So it would be: (pseudo code) r.Id = id AND r.IsActive = True

spinon
Also here is a link to a similar question on SO. http://stackoverflow.com/questions/2500972/linq-to-sql-how-to-efficiently-do-either-an-and-or-an-or-search-for-multiple-cr
spinon
Thanks for the answer... I've put an answer at the bottom as to exactly how I figured it out, but I'll give you credit cuz you took the time to help.
rockinthesixstring
Thanks man. Yeah I just looked up this link:http://msdn.microsoft.com/en-us/vbasic/bb737944.aspx#5 shows your style you listed below.
spinon
hmm I'm gonna have to flag this for future consideration. It appears that both answers are correct, but I wonder about performance. (I came up with my answer by "winging it")
rockinthesixstring
A: 

This seems to be working the way I want it to.

    Public Function GetRegionById(ByVal id As Integer) As Region Implements IRegionService.GetRegionById
        Return _RegionRepository.GetRegions() _
            .Where(Function(r) r.ID = id) _
            .Where(Function(r) r.isActive = True) _
            .FirstOrDefault()
    End Function
rockinthesixstring
@msarchet's answer works swell.. can anybody tell me the difference between his and mine?
rockinthesixstring
+1  A: 

You need to put your predicate inside of ()

Like so

 Public Function GetRegionById(ByVal id As Integer) As Region Implements IRegionService.GetRegionById
        Return _RegionRepository.GetRegions() _
         .Where(Function(r) (r.ID = id And r.isActive = True)).FirstOrDefault
    End Function

The reason for this is it has to return as a boolean. Edit in response to the comments I'm not 100% on this but I think your way performs a where twice for each object, basically it'sgoing to always do an and and compare both values against the queried object, where mine will only compare until a condition is false. Also I'm not sure if you can perform Or operations using your method.

msarchet
@msarchet, your answer works.. can you tell me the difference between yours and mine?
rockinthesixstring
@rockinthesixstring, see my edit
msarchet