1

I am stuck on a type matching problem. I started by ensuring that my syntax was correct by checking it against the following:

Proper Join/GroupJoin implementation

I tried to substitute for all of my conditions, and noticed that my types didn't match. Here is my code:

        var waQ = someRepository.GetAllQ();
        var wrQ = someOtherRepository.GetAllQ();

        query = waQ
            .GroupJoin(wrQ,
                    wa => new { wa.someInt, wa.someNullableInt },
                    wr => new { wr.someMatchingInt, wr.someNonNullableInt },
                    (wa, wr) => new { wa, 
                        reqOrderID = wr.FirstOrDefault().someMatchingInt,
                        reqWorkerID = wr.FirstOrDefault().someNonNullableInt
                    })
            .Select([etc.]);

The error that I am getting, specifically, is that the type arguments cannot be inferred from the usage. I believe the underlying error is that I can't match the nullable int to the non-nullable int.

However, I am hard pressed to find workarounds. I tried casting the non-nullable int "as int?" and I got this error:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

I also tried (int?) and tried casting them both as strings (.ToString()). No luck.

Does anyone have any suggestions?

Before you suggest it, I am not allowed to go and make wa.someNullableInt non-nullable, nor would it make sense for the program.

Thanks in advance for any help!

Community
  • 1
  • 1
Chaim Eliyah
  • 2,743
  • 4
  • 24
  • 37

2 Answers2

1

You should be able to cast it to int? but you'll need to provide a name for the field, rather than having it automatically picked up as your existing syntax is setup. You did mention trying this, but it's not clear what's meant by "and IntelliSense didn't like that." I imagine what you meant is you got this error:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

You should be able to cast it as follows:

wr => new
{
    wr.someMatchingInt,
    someNonNullableInt = (int?)wr.someNonNullableInt
}

The someNonNullableInt = part will be the given field name for that item.

Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
1

The problem is, that second and third arguments are Func<TOuter, TKey> and Func<TInner, TKey>. They should have exactly the same return type TKey. You have two different anonymous types new { wa.someInt, wa.someNullableInt } and new { wr.someMatchingInt, wr.someNonNullableInt }. To create one type you should give names for fields and cast to similar types.

query = waQ
        .GroupJoin(wrQ,
                wa => new { si = wa.someInt, nsi = wa.someNullableInt },
                wr => new { si = wr.someMatchingInt, nsi = (int?)wr.someNonNullableInt },
                (wa, wr) => new { wa, 
                    reqOrderID = wr.FirstOrDefault().someMatchingInt,
                    reqWorkerID = wr.FirstOrDefault().someNonNullableInt 
                })
        .Select([etc.]);
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
  • Success! Thank you! After I gave them the same field names (in this example, si, nsi) the error went away and I was able to compile. – Chaim Eliyah Sep 30 '13 at 04:24