0

I M New to Linq, I M Searching articles using

DBSearchDataContext db = new DBSearchDataContext();

        object q = from b in db.Articles
                   where b.Tags.Contains(val) |
                   b.NewsTitle.Contains(val) |
                   b.EnglishContent.Contains(val)
                   select b;

I Can not exclude results where category = video or photos

Please help me to exclude category where value is "videos" or "Photographs"

4 Answers 4

1
DBSearchDataContext db = new DBSearchDataContext();          
object q = from b in db.Articles                    
           where 
           (b.Tags.Contains(val) ||                    
           b.NewsTitle.Contains(val) ||                    
           b.EnglishContent.Contains(val)) && 
           !( b.SomeCategoryList.Containts("videos") || b.SomeCategoryList.Contains("photos") )
           select b; 
Sign up to request clarification or add additional context in comments.

1 Comment

I'd stay away from the single pipe '|' or operators also. For one thing, they don't do logical short circuiting. Plus I always confuse them for bitwise Or operators.
1

DBSearchDataContext db = new DBSearchDataContext();

    object q = from b in db.Articles
               where b.Tags.Contains(val) |
               b.NewsTitle.Contains(val) |
               b.EnglishContent.Contains(val)
               && ( !b.category.Contains(videos) && !b.category.Contains(Photographs))
               select b;

Maybe something like that....

Comments

1

Actually we need more information about your question. If we say, you have a table called categories with ID PK (int) columns. You need to search as

1 - Videos

2 - Photos

DBSearchDataContext db = new DBSearchDataContext();

    var q = (from b in db.Articles
               where 
               b.CategoryId == 1 // This is returns Videos category
               select b).ToList();

I cant write exact answer, but this will show you a way.

so your query can be;

var q = (from b in db.Articles
                   where 
                   (b.Tags.Contains(val) ||
                   b.NewsTitle.Contains(val) ||
                   b.EnglishContent.Contains(val))
                   &&
                   b.CategoryId == 1
                   select b).ToList();

If you give more information about how you find Category (with relation or enumeration), I can refine my answer.

Comments

0

You should change your | to || which is the proper OR conditional.

To make it exclude, you need something liek this (based on an assumption of where Category is in your DB):

DBSearchDataContext db = new DBSearchDataContext();
       object q = from b in db.Articles
                  where (b.Tags.Contains(val) ||
                         b.NewsTitle.Contains(val) ||
                         b.EnglishContent.Contains(val)) &&
                  !(b.Category != "Videos" && b.Category != "Photos")
                  select b;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.