Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write join with composite key clause in FSharp query expressions?

How to write this C# join with composite key clause in F#? :

join k in DataContext.Catalogs on
   new { id = o.IDENT, v = o.VZ } equals
   new { id = k.IDENT, v = k.VZ }

This is similiar question to this: groupby multiple columns in a F# 3.0 query which is still not answered. But I can't believe it is not easy possible to write it in FSharp.

Thanks

like image 253
tomasK Avatar asked Sep 14 '12 11:09

tomasK


People also ask

How many types of joins in Linq C#?

There are two methods available in Linq to perform Join Operations.

What type of join is Linq join?

In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set.

What is join in asp net?

The Join operator joins two sequences (collections) based on a key and returns a resulted sequence. GroupJoin. The GroupJoin operator joins two sequences based on keys and returns groups of sequences. It is like Left Outer Join of SQL.

How to write join in c#?

A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence. The join clause compares the specified keys for equality by using the special equals keyword.


1 Answers

Use tuples containing the key fields you want:

query {
  for o in DataContext.OTable do
  join k in DataContext.Catalogs on
   ((o.IDENT, o.VZ) = (k.IDENT, k.VZ))
  select (o.IDENT, k.VZ)
}

Note that you can't create an anonymous type with named fields in F#, like you can in C#. Tuples are probably the closest and most idiomatic translation. See Tomas's answer here.

like image 69
latkin Avatar answered Nov 15 '22 12:11

latkin