user2058252
user2058252

Reputation: 23

Translating a Linq query from vb.net to c#

What would be the code equivalent of this to c#?

VB code

Dim q = From p In commDS.Tables(1).AsEnumerable() _
                    Join e In ds.Tables(1).AsEnumerable() On p.Field(Of Integer)("JobID") Equals e.Field(Of Integer)("JobID") And _
                    p.Field(Of Integer)("EventID") Equals e.Field(Of Integer)("EventID") _
                    Select New With {Key .resRow = p, Key .eRow = e}

Below is what I'm trying but it's quite wrong.

 var q = (from p in commDs.Tables[1].AsEnumerable()
                     join e in ds.Tables[1].AsEnumerable() on 
                     p.Field<int>("JobID") equals e.Field<int>("JobID")
                     && e.Field<int>("EventID") equals p.Field<int>("EventID")
        Select new{ p,e}
        );

Upvotes: 1

Views: 86

Answers (1)

D Stanley
D Stanley

Reputation: 152501

I didn't realize VB would let you join on multiple values like that. In C# the equivalent would be to create an anonymous type for the join key(s):

 var q = (from p in commDs.Tables[1].AsEnumerable()
                     join e in ds.Tables[1].AsEnumerable()
                     on new {JobID = p.Field<int>("JobID"), EventID = p.Field<int>("EventID") }
                       equals new {JobID = e.Field<int>("JobID"), EventID = e.Field<int>("EventID") }
        select new {p,e}
        );

The only other difference is that the resulting anonymous type has different field names (p and e versus resRow, eRow), but that's easy enough to change.

Upvotes: 1

Related Questions