Jaj
Jaj

Reputation: 98

Filtering Sql table Data in Linq

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"

Upvotes: 0

Views: 202

Answers (4)

cjk
cjk

Reputation: 46415

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;

Upvotes: 0

Serkan Hekimoglu
Serkan Hekimoglu

Reputation: 4284

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.

Upvotes: 1

jcvpacheco
jcvpacheco

Reputation: 101

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....

Upvotes: 1

asawyer
asawyer

Reputation: 17808

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; 

Upvotes: 1

Related Questions