Conditional operator in Linq Expression causes NHibernate exception

后端 未结 1 690
小蘑菇
小蘑菇 2021-01-18 06:49

I\'m trying to implement search functionality in an ASP.NET MVC 2 application. I create an expression based on criteria entered by the user:

public ViewResul         


        
相关标签:
1条回答
  • 2021-01-18 07:10

    What about dynamically create your query? Like this:

    var customers = CustomerRepository.AllEntities();
    
    if (!forename.IsNullOrEmpty())
        customers = customers.Where(p => p.Forename == forename);
    if (!familyname.IsNullOrEmpty())
        customers = customers.Where(p => p.FamilyNames.Any(n => n.Name==familyname));
    if (dob.HasValue)
        customers = customers.Where(p => p.DOB == dob);
    

    I don't know if this works but I think this could be more efficient.

    0 讨论(0)
提交回复
热议问题