LINQ to SQL and Join two tables with OR clause?

In order to achieve your desired result, you need to do the predicate filterings and joins in on single statement Dim myCriteria() = {"test", "bed"} Dim test = from d in _context. Documents _ join p in _context. Plans on d.ID = p.

ID _ where (myCriteria. Contains(d.Name) OR _ myCriteria. Contains(d.

Descrition)) _ OR (myCriteria. Contains(p. Name) OR _ myCriteria.

Contains(p. Description)) _ select Document = d. Name, Plan = p.Name.

In order to achieve your desired result, you need to do the predicate filterings and joins in on single statement. Dim myCriteria() = {"test", "bed"} Dim test = from d in _context. Documents _ join p in _context.

Plans on d. ID = p. ID _ where (myCriteria.

Contains(d. Name) OR _ myCriteria. Contains(d.

Descrition)) _ OR (myCriteria. Contains(p. Name) OR _ myCriteria.

Contains(p. Description)) _ select Document = d. Name, Plan = p.Name.

This is happening because you are doing a "first pass" which filters the plans and documents that match your predicates, and then joining those results only, effectively doing an AND. Like Basilio said, you should do your join/filter in the same pass. You might try something like this: Dim test = From d in _context.

Documents _ Join p in _context. Plans on d. ID Equals p.ID _ Where predicate1(p) Or predicate2(d) Select d.

Name, p. Name Or similarly: Dim test = From d in _context. Documents _ From p in _context.

Plans _ Where d. ID = p.ID And (predicate1(p) Or predicate2(d)) Select d. Name, p.Name.

I like your answer too. +1 – Jose Basilio Apr 25 '09 at 22:02.

I have structured the where clause for each using PredicateBuilder. So, myPlans and myDocuments have a correct SQL statement. What I'd like to do is join these two tables into one linq statement.

The problem I'm having is that, by default the AND condition is joining the where clauses. MyPlans Where clause : (p.name like "%test%" AND p.name like "%bed%") OR (p. Description like "%test%" AND p.

MyDocuments Where clause : (d.name like "%test%" AND d.name like "%bed%") OR (d. Description like "%test%" AND d. (myplans where clause above) OR (mydocument where clause above).

Meaning, I'd like the two where clauses in each of the tables to be "or" instead of "And". (myplans where clause above) AND (mydocument where clause above). Meaning, I'd like the two where clauses in each of the tables to be "or" instead of "And".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions