I have a List
I have a DB table with the same 3 fields. I need to compare PieceID to make sure the are the same using LINQ. How do I get the table into a List or something so I can use it. I tried datareader and datatable and it does not seem to be able to use them.
I know that I have to use a Join, but I cannot even start to figure out the query until I can use the DB fields.
Please help.
Thanks for your help ahead of time.
Arep

VulpesRank #55 · 29.2K reputationPosted Mar 3, 2012, 10:56 AM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 4, 2012, 12:07 PM EST
I get the error:
Error 1 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
static void CheckDB(ListDetail> fileList, SqlConnection cn) { SqlCommand cmd = new SqlCommand(@"SELECT JobNumber, PieceID, ClaimNbr FROM DentalMemberEOB.dbo.HeaderHistory", cn); cmd.CommandTimeout = 60000; SqlDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); dr.Dispose(); cmd.Dispose(); var query = from list in fileList join db in dt.Rows on list.ClaimNbr equals row["ClaimNbr"] select (int)Row["PieceID"]; }VulpesRank #55 · 29.2K reputationPosted Mar 4, 2012, 12:22 PM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 4, 2012, 12:44 PM EST
VulpesRank #55 · 29.2K reputationPosted Mar 4, 2012, 12:50 PM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 4, 2012, 5:08 PM EST
VulpesRank #55 · 29.2K reputationPosted Mar 5, 2012, 12:14 AM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 5, 2012, 5:11 AM EST
Arep
A RepaskyRank #8,108 · 228 reputationPosted Mar 5, 2012, 5:37 AM EST
var query = from list in fileList join DataRow db in dt.Rows on list.ClaimNbr equals db["ClaimNbr"].ToString() where list.PieceID != db["PieceID"].ToString() select db["ClaimNbr"]; foreach (var result in query) { Output_Files.PrintError("PieceID does not match for ClaimNbr " + result.ToString()); }VulpesRank #55 · 29.2K reputationPosted Mar 5, 2012, 6:30 AM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 5, 2012, 6:45 AM EST
where list.PieceID != (int)db["PieceID"]
to
where list.PieceID != db["PieceID"].ToString()
And I changed the write to console.writeline.
Thanks,
Arep
A RepaskyRank #8,108 · 228 reputationPosted Mar 6, 2012, 11:55 AM EST
I got interrupted and just got back to this. I was doing some testing after you helped me get this code to compile. The PieceID in the file (list) and that on the DB are the same, but I get my error message on every ClaimNbr. I should never get this message.
PieceID does not match for ClaimNbr 00013E9908104 where the file has 000001 and the database has 1
It is like it is doing the opposite that it is supposed to.
So here is the code.
SqlCommand cmd = new SqlCommand(@"SELECT JobNumber, PieceID, ClaimNbr FROM DentalMemberEOB.dbo.HeaderHistory", cn); cmd.CommandTimeout = 60000; SqlDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); dr.Dispose(); cmd.Dispose(); var query = from list in fileList join DataRow db in dt.Rows on list.ClaimNbr equals db["ClaimNbr"].ToString() where list.PieceID != db["PieceID"].ToString() select new { ClaimNbr = list.ClaimNbr, ListPieceID = list.PieceID, DbPieceID = (int)db["PieceID"] }; foreach (var result in query) { Output_Files.PrintError("PieceID does not match for ClaimNbr " + result.ClaimNbr + " where the file has " + result.ListPieceID + " and the database has " + result.DbPieceID); }A RepaskyRank #8,108 · 228 reputationPosted Mar 6, 2012, 12:23 PM EST
on list.ClaimNbr equals db["ClaimNbr"].ToString()
So I have to figure out how to do that. I will start looking online.
How can I do that?
A RepaskyRank #8,108 · 228 reputationPosted Mar 6, 2012, 12:59 PM EST
var query = from list in fileList join DataRow db in dt.Rows on new { list.ClaimNbr, list.JobNumber } equals new { DbClaimNbr = db["ClaimNbr"], DbJobNumber = db["JobNumber"] } where list.PieceID != db["PieceID"].ToString() select new { ClaimNbr = list.ClaimNbr, ListPieceID = list.PieceID, DbPieceID = (int)db["PieceID"] }; But the join has an error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'. Does anyone have an idea how to join on multiple columns? I add the JobNumber is all. ArepVulpesRank #55 · 29.2K reputationPosted Mar 6, 2012, 1:29 PM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 7, 2012, 4:51 AM EST
I see what you are talking about that PieceID would never compare. I also see what you mean about the types. So I copied your code over into the program. It gets the same type error.
Arep
VulpesRank #55 · 29.2K reputationPosted Mar 7, 2012, 6:23 AM EST
So, we know that this works:
var query = from list in fileList
join DataRow db in dt.Rows
on list.ClaimNbr equals db["ClaimNbr"].ToString()
select (int)db["PieceID"];
but this doesn't work:
var query = from list in fileList
join DataRow db in dt.Rows
on new { list.ClaimNbr, list.JobNumber }
equals new { DbClaimNbr = db["ClaimNbr"].ToString(), DbJobNumber = db["JobNumber"].ToString() }
where list.PieceID != db["PieceID"].ToString("D6")
select new { ClaimNbr = list.ClaimNbr, ListPieceID = list.PieceID, DbPieceID = db["PieceID"].ToString("D6") };
So that tells us that:
1. It can't be anything to do with the first two lines as they're the same in both cases.
2. list.ClaimNbr is definitely a string otherwise it could never be equal to db["ClaimNbr"].ToString().
The only remaining possibility is that list.JobNumber is not a string. Could it be an int, perhaps ?
A RepaskyRank #8,108 · 228 reputationPosted Mar 7, 2012, 6:53 AM EST
var query = from list in fileList join DataRow db in dt.Rows on new { list.ClaimNbr, list.JobNumber } equals new { ClaimNbr = db["ClaimNbr"].ToString(), JobNumber = db["JobNumber"].ToString() } where list.PieceID.ToString() != db["PieceID"].ToString() select new { ClaimNbr = list.ClaimNbr, ListPieceID = list.PieceID.ToString(), DbPieceID = db["PieceID"].ToString() }; Now it compiles and runs. But the where clause is not working correctly, because it sees list.PieceID as a string of 6 characters and db["PieceID"] it sees as 1 character. In the ToString it gives an error if I use "D6". Maybe I will try a substring to make it 6 characters? I get back on that. ArepVulpesRank #55 · 29.2K reputationPosted Mar 7, 2012, 7:03 AM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 7, 2012, 7:04 AM EST
var query = from list in fileList join DataRow db in dt.Rows on new { list.ClaimNbr, list.JobNumber } equals new { ClaimNbr = db["ClaimNbr"].ToString(), JobNumber = db["JobNumber"].ToString() } where list.PieceID.ToString() != db["PieceID"].ToString().PadLeft(6, '0') select new { ClaimNbr = list.ClaimNbr, ListPieceID = list.PieceID.ToString(), DbPieceID = db["PieceID"].ToString().PadLeft(6, '0') }; Now I going to try to do the same but check for an int instead. ArepVulpesRank #55 · 29.2K reputationPosted Mar 7, 2012, 7:56 AM EST
A RepaskyRank #8,108 · 228 reputationPosted Mar 7, 2012, 8:54 AM EST
var query = from list in fileList join DataRow db in dt.Rows on new { list.ClaimNbr, list.JobNumber } equals new { ClaimNbr = db["ClaimNbr"].ToString(), JobNumber = db["JobNumber"].ToString() } where Convert.ToInt32(list.PieceID) != (int)db["PieceID"] select new { ClaimNbr = list.ClaimNbr, ListPieceID = Convert.ToInt32(list.PieceID), DbPieceID = (int)db["PieceID"] }; Vulpes, thanks for your help. Arep