2

I want to join 2 tables, which they have 2 same columns. I have tried this but got error with on new {} equals new {}.

My Code:

var results = from table1 in table.AsEnumerable()
    join table2 in comment.AsEnumerable()
    //Comment
    on new {table1.Field<string>("SignalName"), table1.Field<int?>("MessageID")} 
    equals new {table2.Field<string>("SignalName"), table2.Field<int?>("MessageID")} 

    into prodGroup
    from table3 in prodGroup.DefaultIfEmpty()

    select new
    {
       // something is there
    };
0

2 Answers 2

4

Try...

var results = from r1 in table.AsEnumerable()
              join r2 in comment.AsEnumerable()
              on new {
                        signal=r1.Field<string>("SignalName"), 
                        message=r1.Field<int?>("MessageID")
               } 
              equals new {
                        signal=r2.Field<string>("SignalName"), 
                        message=r2.Field<int?>("MessageID")
              } into prodGroup
              from r3 in prodGroup.DefaultIfEmpty();
Sign up to request clarification or add additional context in comments.

Comments

1

Try to name the properties of your anonymous types.

From MSDN:

Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.