In LINQ Join operator, how does one specify multi-column keys?

  • Thread starter Thread starter Juan Dent
  • Start date Start date
J

Juan Dent

Hi,

I have the following snippet:
--------------
var employeeOptions = employees
.Join(
empOptions, // inner sequence
e => e.id, // outerKeySelector
o => o.id, // innerKeySelector
(e, o) => new // resultSelector
{
id = e.id,
name = string.Format("{0} {1}", e.firstName, e.lastName),
options = o.optionsCount
});
---------

in this case the joining keys "happen" to be composed of only one
property/column. I am concerned as how to deal with the other posibility:
multi-column keys!
What is the syntax for that? - suppose the keys are the id and the city,
then what lambda expressions would I use?

e => { e.id, e.city} , // outerKeySelector
o => { o.id o.city}, // innerKeySelector

or perhaps using anonymous types - adding a new to the braces:

e => new { e.id, e.city} , // outerKeySelector
o => new { o.id o.city}, // innerKeySelector

???
 
Juan said:
Hi,

I have the following snippet:
--------------
var employeeOptions = employees
.Join(
empOptions, // inner sequence
e => e.id, // outerKeySelector
o => o.id, // innerKeySelector
(e, o) => new // resultSelector
{
id = e.id,
name = string.Format("{0} {1}", e.firstName, e.lastName),
options = o.optionsCount
});
---------

in this case the joining keys "happen" to be composed of only one
property/column. I am concerned as how to deal with the other posibility:
multi-column keys!
What is the syntax for that? - suppose the keys are the id and the city,
then what lambda expressions would I use?

e => { e.id, e.city} , // outerKeySelector
o => { o.id o.city}, // innerKeySelector

or perhaps using anonymous types - adding a new to the braces:

e => new { e.id, e.city} , // outerKeySelector
o => new { o.id o.city}, // innerKeySelector

???

I find the extension method approach for .Join incredibly hard to write
for more advanced usages, so I'll revert to the normal C# way.

You indeed have to specify an anonymous type. Anonymous types are
considered equal if they have the same properties (name, type) and the
CTor is in the same order.

So say you have two tables: PKSide and FKSide. PKSide has a compound
PK, ID1, and ID2. FKSide has an FK to PKSide using the FK fields FkID1
and FkID2. If you now want to join these in a linq query you do:

var q = from pk in ctx.PkSide
join fk in ctx.FkSide on
new { ID1=pk.ID1, ID2=pk.ID2} equals
new { ID1=fk.FkID1, ID2=fk.FkID2}
// rest of query as normal.


FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Hi Juan,

The selection of the keyselector depends on your requirement. The
outerkeyselector and the innerkeyselector are used to be compared
internally. If the outerkeyselector of the outer item equals to the
innerkeyselector of the inner item current linked item will be added to the
result collection.

You can try following code in ASP.NET Web Project to test:

Aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Employee> list1 = new List<Employee>();
List<Employee> list2 = new List<Employee>();


Employee x0 = new Employee() { ID = 1, ID2=1, Name = "Name"
};
Employee x1 = new Employee() { ID = 1, ID2 = 2, Name =
"Name2" };
list1.Add(x0);
list1.Add(x1);

Employee x2 = new Employee() { ID = 1, ID2 = 1, Name =
"Name3" };
Employee x3 = new Employee() { ID = 1, ID2 = 2, Name =
"Name4" };
Employee x4 = new Employee() { ID = 1, ID2 = 2, Name =
"Name5" };
list2.Add(x2);
list2.Add(x3);
list2.Add(x4);

this.GridViewl1.DataSource = list1;
this.GridViewl1.DataBind();
this.GridViewl2.DataSource = list2;
this.GridViewl2.DataBind();


var q = list1.Join(list2, x => MyOuterKeySelector(x.ID, x.ID2),
o => MyInnerKeySelector(o.ID, o.ID2), (x, o) => new { list1_id = x.ID,
list1_id2 = x.ID2, list2_id = o.ID, list2_id2 = o.ID2, list1_name = x.Name,
list2_name = o.Name });
this.GridViewResult.DataSource = q;
this.GridViewResult.DataBind();
}

object MyOuterKeySelector(object o, object o2)
{
//decide the key selector based on your requirement.
//in this case I can convert it to string and insert a custom
separator
return o + "_" + o2;
}
object MyInnerKeySelector(object o, object o2)
{
//decide the key selector based on your requirement.
//in this case I can convert it to string and insert a custom
separator
return o + "_" + o2;
}
}
public class Employee {
public int ID { get; set; }
public int ID2 { get; set; }
public string Name { get; set; }
}

Aspx:
list1<br />

<asp:GridView ID="GridViewl1" runat="server">
</asp:GridView>
list2<br />
<asp:GridView ID="GridViewl2" runat="server">
</asp:GridView>
Join<br />
<asp:GridView ID="GridViewResult" runat="server">
</asp:GridView>

You can see it's free for you to choose the keyselector.
Please let me know if you have further questions.

Regards,
Allen Chen
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

What FB said can also work. One thing to consider is the performance.
Comparing anonymous type is not as efficient as comparing string. See the
following test:

Stopwatch stopWatch1 = new Stopwatch();
Stopwatch stopWatch2 = new Stopwatch();


TimeSpan ts1;
TimeSpan ts2;

protected void Page_Load(object sender, EventArgs e)
{
var q = new { ID = "1", ID2 = "2" };
var q1 = new { ID = "1", ID2 = "2" };
string s = "1_2";
string s2 = "1_2";
stopWatch1.Start();
bool b = q.Equals(q1);
stopWatch1.Stop();
// Get the elapsed time as a TimeSpan value.

stopWatch2.Start();
bool b2 = s.Equals(s2);
stopWatch2.Stop();

ts1 = stopWatch1.Elapsed;
ts2 = stopWatch2.Elapsed;
//Set breakpoint here
}

On the other hand, we need to choose the separator string cautiously to
avoid ambiguity (consider the string of the property data starts or ends
with the separator string).

Regards,
Allen Chen
Microsoft Online Community Support
 
Hi Juan,

Have you got the expected answer?

Regards,
Allen Chen
Microsoft Online Community Support
 
Back
Top