Note that there are some explanatory texts on larger screens.

plurals
  1. POpredicate builder with two tables
    text
    copied!<p><img src="https://i.stack.imgur.com/VNahZ.png" alt="enter image description here"></p> <p>A <em>Party</em> can have one or more <em>Contact</em> objects. </p> <p>I want to select all <em>Parties</em> who's streetname contains a specific keyword.<br> If I just want to search in <em>Party</em> I can use the code below. But how do I extend it to also search in <em>Contact</em>? </p> <pre><code>public IQueryable&lt;Party&gt; SearchParties(List&lt;string&gt; keywords) { var predicate = PredicateBuilder.False&lt;Party&gt;(); foreach (string word in keywords) { var keyword = word; predicate = predicate.Or(p =&gt; p.surname.Contains(keyword)); predicate = predicate.Or(p =&gt; p.lastname.Contains(keyword)); predicate = predicate.Or(p =&gt; p.number.Contains(keyword)); } return db.Parties.Where(predicate); } </code></pre> <p>Is there anything else you need to know? </p> <p><strong>EDIT</strong><br> I guess I could create another predicate and then join them afterwards. Something like: </p> <pre><code>var predicate2 = PredicateBuilder.False&lt;Contact&gt;(); </code></pre> <p>...and in the foreach: </p> <pre><code>predicate2 = predicate2.Or(p =&gt; p.streetname.Contains(keyword)); </code></pre> <p>But how would I join predicate and predicate2 before returning? </p> <p><strong>EDIT2</strong><br> Or, join the <em>Party</em> and <em>Contact</em> before doing the predicate Builder? </p> <p><strong>EDIT3</strong><br> Here are parts of the generated classes: </p> <pre><code>[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Contact")] public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private int _id; // ...more properties private EntityRef&lt;Party&gt; _Party; public Contact() { this._Party = default(EntityRef&lt;Party&gt;); OnCreated(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload