LINQ to Entities: Why can't I use Split method as condition?

后端 未结 4 2094
失恋的感觉
失恋的感觉 2020-12-21 02:37

I have the following LINQ query:

var aKeyword = \"ACT\";
var results = from a in db.Activities
              where a.Keywords.Split(\',\').Contains(aKeyword)         


        
4条回答
  •  一个人的身影
    2020-12-21 02:59

    In response to your performance considerations on a big dataset:

    You are going to be doing non indexed wildcard string matching on the client, so yes, there will be performance loss.

    Is there a reason why you have multiple keywords in one table field? You could normalize that out, to have a ActivityKeywords table where for each Activity you have a number of Keyword records.

    Activities(activity_id, ... /* remove keywords field */) ---> ActivityKeywords(activity_id, keyword_id) ---> Keywords(keyword_id, value)

    Check out Non-first normal form: http://en.wikipedia.org/wiki/Database_normalization

    EDIT: Also even if you were to stick with the one column, there is a way to do everything serverside (if you have a strict syntax: 'keyword1, keyword2, ..., keywordN'):

    var aKeyword = "ACT";
    var results = (from a in db.Activities
                  where a.Keywords.Contains("," + aKeyword) || a.Keywords.Contains(aKeyword + ",")
                  select a;
    

提交回复
热议问题