ASP.NET search form - dynamic Linq to SQL?

前端 未结 3 1404
自闭症患者
自闭症患者 2021-02-10 04:27

I have a search form that allows users to search on several different fields in several different ways. Here is an example of my code.

var claims = from c in db.         


        
3条回答
  •  一个人的身影
    2021-02-10 05:23

    You could make a dictionary of your possible predicates:

    Dictionary>>> map = new Dictionary>>>() {
        { "StartsWith", t => c => c.companyFileID.StartsWith(t) },
        { "Equals",     t => c => c.companyFileID == t },
        { "Contains",   t => c => c.companyFileID.Contains(t) }
    };
    

    Which could be used like this:

    var search = ddlSearchField.Text;
    var text = txtSearchBox.Text;
    
    var claims = from c in db.Claims select c;
    
    Func>> predicate = null;
    if(dict.TryGetValue(search, out predicate))
        claims = claims.Where(predicate(text));
    

提交回复
热议问题