0

EDIT - So there was a /n in the string I was trying to query. I love computers.

I need to get products that are in a category based on the categories name. If I use the category ID, it works...

from r in db.products 
from pc in r.productcategories 
where pc.categories.categoryid == n  
select r

but this doesn't

from r in db.products 
from pc in r.productcategories 
where pc.categories.categoryname == n  
select r

I also tried contains...

from r in db.products 
from pc in r.productcategories 
where pc.categories.categoryname.Contains(n)  
select r

There are no errors, but no rows are returned, whereas rows are returned when I use the id.

9
  • 2
    what is n in your query? Commented Apr 15, 2015 at 16:28
  • 3
    for the second one n doesnot carry an id value right? Commented Apr 15, 2015 at 16:28
  • 1
    Equality should work, but what about case-sensitive value of n or the column type you are trying to query against in the database? (varchar, nvarchar, text) Commented Apr 15, 2015 at 16:29
  • If there are no exceptions, it's almost definitely an error in the query itself. Try running the exact same query in direct SQL and see if you get rows. Commented Apr 15, 2015 at 16:30
  • 1
    there was a /n in the string I was trying to query. jfc. Commented Apr 15, 2015 at 20:11

3 Answers 3

1

You need string comparison in where clause, may be right now your n can have id value, so make sure that your n variable have string value

string n = "categoryname"  

from r in db.products 
from pc in r.productcategories 
where pc.categories.categoryname == n  
select r

Second possible case will be your Product Categories will be collection of values

Sign up to request clarification or add additional context in comments.

8 Comments

@MaciejLos: Are you thinking this is SQL code? This seems (at a glance) to be a normal LINQ query...
@Chris, i know that is a linq query. Looks like cross join, instead inner join. OP taged this question as Entity-framework, so... there must be a relationship between both "collections".
The relationships will not be defined on each query. They are part of the setup. The second from references the thing from the first....
Oh my Gosh, @Chris, i'm talking about query performance. Using cross joins might not to be intended by OP. I'm not question the usage, i'm going to show the difference...
@MaciejLos: As Juharr says only if they are not related. The whole point of entity frameworks is that your database object (ie db here) knows everything it needs to about the relationship between the tables. It is defined in the setup of the system. This is why I thought that maybe you thought the code was SQL because in SQL you have to define those relationships in every query. In entity framework they are already there which is why the code above is perfectly reasonable and perfectly idiomatic LINQ2SQL.
|
1
from r in db.products 
from pc in r.productcategories 
where pc.categories.categoryname.Contains(n)  || n.Contains(pc.categories.categoryname)
select r

Give that a try, may be n contains some extra characters like empty space or tab

Comments

0

If n is integer ID & CategoryName is string then you are comparing wrong things.

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.