I have checked lots of posts having this error but none had this particular problem. (Also I am new to C sharp have been a java dev)
I am getting an exception as
System.InvalidCastException was caught
on the table2.Field("MEME_CK") line below in the code snippet.
There are about 3K rows in the table2, I couldn't find a way to avoid wrong casting for line table2.Field("MEME_CK")
Data can either be null, not present, valid or invalid. So I tried using nullable operator on the generic parameter cast. Also saw there is DBNull class that can possibly represent non existent value.
Is there a way to preprocess column data before doing "equals test" or joining in the code below ? How can I avoid casting even after using nullable type?
Following code basically do a join on two data tables based on MemberID i.e. MEME_CK or MemeCk and creates new object with CapHeadID, MemeCk etc as fields.
var query =
(from table1 in searchResult.AsEnumerable()
join table2 in memberInfo.AsEnumerable()
on table1.Field<decimal?>("MemeCk") equals
table2.Field<decimal?>("MEME_CK")
select new
{
CapHeadID = table1.Field<decimal>("CapHeadID"),
MemeCk = table1.Field<decimal>("MemeCk"),
Suffix = table2.Field<decimal>("MEME_SFX"),
Suscriber = table2.Field<string>("SBSB_ID"),
BusinessArea = table2.Field<string>("TEAM"),
MemberName = table2.Field<string>("MemberName"),
WorkTypeName = table1.Field<string>("WrkName"),
SSN = table2.Field<string>("MEME_SSN"),
AssignedUser = table1.Field<string>("AssignedUser")
}).Distinct().OrderBy(a => (a.Suscriber.IsNotNil() ?
a.Suscriber : "")).Take(3000);
MEME_CK
? – D Stanley