ASP.NET MVC2 Linq Where Clause using StartsWith

前端 未结 2 467
无人及你
无人及你 2021-01-26 00:10

I have a few filters on my view, the first one is list by first name, last name and company name when one of these options are selected the user can then select a, b, c ... x, y

2条回答
  •  甜味超标
    2021-01-26 00:48

    Ok after our long comments below, why don't you just chain the linq statements like below?

    if (collection["Filter"] == "2") { 
       presentations = presentations.Where(x => x.Person.FirstName.StartsWith("A")).
           OrderBy(x => x.Person.FirstName);
    }
    

    Since the Where and the OrderBy are deferred until you actually do something with the query like a ToList(), try doing:

    var orderedData = presentations.ToList();
    

    Inspect it, it should be in the correct order as I can't see anything wrong with your linq other than the code you posted never actually is executed until you do a Select or ToList or something with it.

提交回复
热议问题