{"action":"opened","issue":{"url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/47","labels_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/47/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/47/comments","events_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/47/events","html_url":"https://github.com/Microsoft/visualfsharp/issues/47","id":54690594,"number":47,"title":"Tuples in query expression with grouping cannot be translated to SQL","user":{"login":"KevinRansom","id":5175830,"avatar_url":"https://avatars.githubusercontent.com/u/5175830?v=3","gravatar_id":"","url":"https://api.github.com/users/KevinRansom","html_url":"https://github.com/KevinRansom","followers_url":"https://api.github.com/users/KevinRansom/followers","following_url":"https://api.github.com/users/KevinRansom/following{/other_user}","gists_url":"https://api.github.com/users/KevinRansom/gists{/gist_id}","starred_url":"https://api.github.com/users/KevinRansom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KevinRansom/subscriptions","organizations_url":"https://api.github.com/users/KevinRansom/orgs","repos_url":"https://api.github.com/users/KevinRansom/repos","events_url":"https://api.github.com/users/KevinRansom/events{/privacy}","received_events_url":"https://api.github.com/users/KevinRansom/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-01-18T08:52:13Z","updated_at":"2015-01-18T08:52:13Z","closed_at":null,"body":"Opened by tomasp on CodePlex\r\n\r\nWhen answering  this StackOverflow question, I came across an odd behavior that appears to be a bug:\r\n```fsharp\r\n#r \"FSharp.Data.TypeProviders.dll\"\r\n#r \"System.Data.Linq.dll\"\r\nopen System\r\nopen System.Data\r\nopen System.Linq\r\nopen Microsoft.FSharp.Data.TypeProviders\r\n\r\ntype dbSchema = SqlDataConnection< @\"Server=.\\SQLExpress;AttachDbFilename=C:\\Temp\\Northwind.mdf;Database=dbname;Trusted_Connection=Yes;\">\r\nlet db = dbSchema.GetDataContext()\r\n\r\nquery {\r\n    for p in db.Products do\r\n    join c in db.Categories on (p.ProductID = c.CategoryID)\r\n    groupBy p.CategoryID.Value into g\r\n    select (g.Key, g.Select(fun (p, c) -> if p.ProductName = c.CategoryName then 1.0 else 0.0).Average()) }\r\n|> Array.ofSeq\r\n```\r\nRunning this gives the following message:\r\n\r\nSystem.InvalidOperationException: Member access 'Products Item1' of 'System.Tuple2[Products,Categories]' not legal on type 'Microsoft.FSharp.Linq.RuntimeHelpers.Grouping2[System.Int32,System.Tuple`2[Products,Categories]].\r\n\r\nIt appears that somewhere during the translation, the translator attempts to generate (or process) code that accesses the Item1 property (which should be a property of the elements in the collection) as if it was a property of the entire grouping. \r\n\r\nIs the F# library doing some pre-processing here? I remember that tuples were always a problem in F# queries, because they are immutable and so they behave differently than C# anonymous types (which the translator handles well)...\r\n\r\n>comments\r\n>latkin wrote Apr 24, 2014 at 12:56 PM [x] \r\n>Yes, we have rather nasty issues that arise due to immutable/mutable tuple conversions in queries. See this issue, for example.\r\n>>tomasp wrote Apr 25, 2014 at 8:44 AM [x] \r\n>>Thanks for the clarification! This makes sense - I remember trying to get that to work in some earlier versions of F# LINQ and it was pretty messy.\r\n>>Could someone who knows the code share a few pointers to the relevant bits in the F# codebase? >>This sounds like an issue that people from the F# community could help to fix (because it is quite separate stand-alone component that can be modified without knowing everything about the compiler :-)).\r\n>>I'm sure that with a bit of help & starting points, we could get a nice pull request for this one!\r\n>>>dsyme wrote Apr 28, 2014 at 7:28 AM [x] \r\n>>>The general area is here:\r\n>>>[https://github.com/fsharp/fsharp/blob/65eb229125713caa2a97d85e6541313f9da4d9f7/src/fsharp/FSharp.Core/Query.fs#L1065](https://github.com/fsharp/fsharp/blob/65eb229125713caa2a97d85e6541313f9da4d9f7/src/fsharp/FSharp.Core/Query.fs#L1065)\r\n>>>The implementation is non-trivial I'm afraid, one of the hardest parts of FSharp.Core \r\n>>>>vladima wrote Apr 28, 2014 at 9:21 AM [x] \r\n>>>>Some time ago I've prototyped implementation that 1) converts quotations to the shape similar to ETs produced by C# compiler 2) delays translation Tuple <-> AnonymousObject to the last step so query composition should not be affected by it. Maybe it is time to publish it as possible alternative that can replace existing implementation somewhere in the future (far far away)"}}