views:

255

answers:

1

I am desperately trying to use LinqKits PredicateBuilder to allow the user to enter a search term into a text box and return records from two database tables/entitysets but I'm struggling to get anywhere. The (simplified) database structure is as follows:

Person                Alias
------                ------
                      A_ID
P_ID ---------------< P_ID
P_FIRST_NAME          A_FIRST_NAME
P_SURNAME             A_SURNAME

So, each person can have 0 or many aliases. What I am trying to do is allow the user to search on a name and pull back the rows from the Person table where that name matches that in either the Person or the Alias table. So far I have got:

        var peopleQuery = MainFrm.genesisContext.People.AsExpandable();
        var peoplePredicate = PredicateBuilder.True<Person>();

        var aliasQuery = MainFrm.genesisContext.Alias.AsExpandable();
        var aliasPredicate = PredicateBuilder.False<Alias>();

        if (!String.IsNullOrEmpty(txtFirstName.Text.Trim()))
        {
            peoplePredicate = peoplePredicate.And(p => p.P_FIRST_NAME == txtFirstName.Text);

            aliasPredicate = aliasPredicate.And(a => a.A_FIRST_NAME == txtFirstName.Text);

            peoplePredicate = peoplePredicate .Or(p => aliasPredicate);
        }

This doesn't work because I'm trying to convert from People to Alias. Basically I'm completely stuck and not sure whether it is even possible to do an Or query on two different tables(?)

+1  A: 

I can think of several possible solutions:

  1. Include the primary name of each person in the Alias table, and just search on the Alias table, or
  2. UNION the two tables together before the search, or
  3. Run a Linq query that outer joins the Alias table to the Person table, and use the result in your search, including your OR condition
Robert Harvey
How would I do an outer join in linq? I thought that outer joins were not possible?
Calanus
Simulating an Outer Join: http://www.hookedonlinq.com/OuterJoinSample.ashx
Robert Harvey
Thanks, In the end I used the Linq Union operator to join the two result sets together...
Calanus