0

I work on an ASP.NET MVC4 solution.

I have a page with search criteria, something like this:

  • Transport number from : _ _ _ _ _
  • Transport number to : _ _ _ _ _ _

Now I would like to query my model for that range.

[DataContract]
public class SearchParametersTransportDTO
{
    [DataMember]
    public string TransportNumberFrom { get; set; }

    [DataMember]
    public string TransportNumberTo { get; set; }
}


public SearchResultDTO<Transport> SearchTransports(SearchParametersTransportDTO dto)
{
    using (var unitOfWork = UnitOfWorkFactory.Create())
    {
        var transportRepository = unitOfWork.Create<Transport>();
        var transports = transportRepository.GetAll();
        transports = transports.Where(s => s.TransportNumber.Contains(dto.TransportNumberFrom));
        ...
    }
    ...

I don't find any way for searching for the specific range from...to.

Please note that my transport number is not an int but a string (eg: 'AZE.12/0009', 'AZE.12/0010', ...)

Any idea?

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bronzato
  • 9,438
  • 29
  • 120
  • 212
  • Can you clarify where `dto` came from, and what is `TransportNumberFrom`? Also, do all transport numbers share the same exact format (XXX.NN/NNNN)? – PinnyM Feb 15 '13 at 14:15
  • I updated my question to be more precise about dto. TransportNumber is earch criteria in my dto. And yes, all transport numbers share the same format. Thanks. – Bronzato Feb 15 '13 at 14:19

1 Answers1

0

A related question has this answer: LINQ Between Operator

It might be overkill for what you want, but the expression of the query surely is nice an clean.

var query = transports.Between(
            t => 
            t.TransportNumber, TransportNumberFrom, TransportNumberTo);
Community
  • 1
  • 1
scott-pascoe
  • 1,463
  • 1
  • 13
  • 31
  • There are some caveats to this code. It should work with strings when you are going directly to a IQueryable data source, but if you are trying to do strings to an IEnumerable data source, it will blow up. – scott-pascoe Feb 15 '13 at 15:50