0

I have two tables; EndToEnd and PartPort. I'd like to get the PartPortA and PartportB data from the same row in EndToEnd, and query Partport with them and get their corresponding PartGid from Partport which could be on any row in the Partport table. So far, I'm able to do this, but I have to do two different LINQ calls but I'd like to reduce it down to one. Here is my code:

    // this demonstrates how to join two tables, however only works for one AssetportGid at a time
    var part_portGid_a_results = (from icp in entities.EndToEnd
                  where icp.IntertPortGidA != null &&
                  icp.IntertPortGidB != null
                  join ica in entities.PartPort
                  on icp.PartPortA   equals ica.PortGid
                  select new { icp.PartPortA, ica.PartGid, }).ToList();

    var part_portGid_b_results = (from icp in entities.EndToEnd
                                   where icp.IntertPortGidA != null &&
                                   icp.IntertPortGidB != null
                                   join ica in entities.PartPort
                                   on icp.PartPortB equals ica.PortGid
                                   select new { icp.PartPortA, ica.PartGid, }).ToList();



    return Json(part_portGid_a_results, JsonRequestBehavior.AllowGet);

What i'd like to do, and I have already tried but got an error is this:

                var part_portGid_a_results = (from icp in entities.EndToEnd
                  where icp.IntertPortGidA != null &&
                  icp.IntertPortGidB != null
                  join ica in entities.PartPort
                  on icp.PartPortA && icp.PartPortB   equals ica.PortGid
                  select new { icp.PartPortA, ica.PartGid, }).ToList();

The error i get is:

Guid? EndToEnd.PartPortB

Error:
    Operator '&&' cannot be applied to operands of type 'System.Guid' and 'System.Guid?'
3
  • "on icp.PartPortA && icp.PartPortB equals ica.PortGid" is not correct, it's not a valid boolean expression, since icp.PartPortA is a Guid, and not a boolean. Are you trying to join on both columns? Then try this instead: "on icp.PartPortA equals ica.PortGid && icp.PartPortB equals ica.PortGid" Commented Aug 23, 2016 at 19:16
  • Intellisense doesnt seem to like that Commented Aug 23, 2016 at 19:23
  • Probably because one is a nullable guid and the other is not ... (Use .Value nullable guid) Commented Aug 23, 2016 at 19:33

1 Answer 1

2

You don't have to use join. If you want to join with a "complex" comparison, just make a Cartesian product (from ... from) and link the rows by a where clause

var part_portGid_results = (from icp in entities.EndToEnd
                            where icp.IntertPortGidA != null &&
                            icp.IntertPortGidB != null
                            from ica in entities.PartPort
                            where icp.PartPortA == ica.PortGid
                               || icp.PartPortB == ica.PortGid
                            select new { icp.PartPortA, ica.PartGid, }).ToList();
Sign up to request clarification or add additional context in comments.

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.