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 ie 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);
You're using AsEnumerable
to convert the queries away from IQueryable... basically this means LINQ won't try to generate SQL code for the join, and the join will be done in C#.
With that in mind, you could try just get them as objects - like this:
from table1 in searchResult.AsEnumerable()
join table2 in memberInfo.AsEnumerable()
on table1.Field<object>("MemeCk") equals
table2.Field<object>("MEME_CK")
select new...
I'm not sure if that will work, but it might do
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.