简体   繁体   中英

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}
        );

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM