kwh3856
asked on
LINQ Query will not hold more than one record at a time. How do you get it to hold multiple records
I have setup a primary loop and a secondary loop using LINQ and for next loops. In the first loop, the result variable shows that it has 7 records in it. I then use this result set to lookup another table based on the data in the first query. When the application loops through the secondary table, it finds the first record that I am actually looking for, next it loops through 6 more times. Each of those records are not a match. The end result is a query result with no data. Then I try to bind that query variable to a datagrid but obviosuly it displays no data. Can someone point out what I am doing wrong in the secondary loop?
var PatAttndByDocResult = from patattndbydocdata in dcRadRelay.PATIENTS_ATTENDED_BY_DOCTORs
where patattndbydocdata.PracticeTIN == MyGlobalVars.RefDocPracticeTIN
select new { patattndbydocdata };
int patattndbydocCount = PatAttndByDocResult.Count(); // <===== Added this line to get total count here
if (patattndbydocCount == 0) // <===== Changed this line so that the query does not get executed again
{
// No record found
}
else
{
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord.patattndbydocdata.MPI == patientdata.MPI &&
(patientdata.FIRST_NAME == tbPatFname.Text ||---<<-------First record matches here-----
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata.FIRST_NAME, patientdata.LAST_NAME, patientdata.DATE_OF_BIRTH, patientdata.ADDRESS1, patientdata.SEX };
this.UltraWebGridPatSrch.DataSource = PatientResult;----<<<<<----First Record shows in PatientResult
ASKER
sree,
The actual result may be one record or multiple records. I need to be able to handle both situations based on the search criteria the user keys in.
Will your logic support that need? I will try the code and let you know. Thank you very much for helping me.
The actual result may be one record or multiple records. I need to be able to handle both situations based on the search criteria the user keys in.
Will your logic support that need? I will try the code and let you know. Thank you very much for helping me.
ASKER
sree,
I tried the code and got the follwing error message
An invalid data source is being used for UltraWebGridPatSrch. A valid data source must implement either IListSource or IEnumerable.
I also tried to do the List function but it wanted to make it listbox. What is the exact syntax to create a list?
Thanks
Kenny
I tried the code and got the follwing error message
An invalid data source is being used for UltraWebGridPatSrch. A valid data source must implement either IListSource or IEnumerable.
I also tried to do the List function but it wanted to make it listbox. What is the exact syntax to create a list?
Thanks
Kenny
This should sort you out:
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord. patattndby docdata.MP I == patientdata.MPI &&
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).ToList();
patientResult_DS.AddRange( (IEnumerab le<PATIENT >)PatientR esult);
this.UltraWebGridPatSrch.D ataSource = patientResult_DS;
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord.
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).ToList();
patientResult_DS.AddRange(
this.UltraWebGridPatSrch.D
ASKER
sree,
When I typed in that first line, I got this error message.
do I need an additional using ????? statement? If so, what should it be?
Error 2 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
When I typed in that first line, I got this error message.
do I need an additional using ????? statement? If so, what should it be?
Error 2 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
Add these to you CS
using System.Collections.Generic ;
using System.Linq;
using System.Collections.Generic
using System.Linq;
ASKER
sree,
now i get
Error 2 'System.Collections.Generi c.List<Cha rtRelay35s p1.PATIENT >' does not contain a definition for 'FIRST_NAME' and no extension method 'FIRST_NAME' accepting a first argument of type 'System.Collections.Generi c.List<Rad tRelay35sp 1.PATIENT> ' could be found (are you missing a using directive or an assembly reference?)
That occurs for
first name
last name
address
city
phone
ssn
now i get
Error 2 'System.Collections.Generi
That occurs for
first name
last name
address
city
phone
ssn
Can you show me your code
ASKER
Here it is
protected void UltraWebGridPatSrch_InitializeDataSource(object sender, UltraGridEventArgs e)
{
bool authenticated;
authenticated = Convert.ToBoolean(Session["authenticated"]);
if (authenticated)
{
RadRelayDataClassDataContext dcRadRelay = new RadRelayDataClassDataContext();
// Read Table "Providers"
var PatAttndByDocResult = from patattndbydocdata in dcRadRelay.PATIENTS_ATTENDED_BY_DOCTORs
where patattndbydocdata.PracticeTIN == MyGlobalVars.RefDocPracticeTIN
select new { patattndbydocdata };
int patattndbydocCount = PatAttndByDocResult.Count(); // <===== Added this line to get total count here
if (patattndbydocCount == 0) // <===== Changed this line so that the query does not get executed again
{
// No user record found
}
else
{
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord.patattndbydocdata.MPI == patientdata.MPI &&
(patientResult_DS. == tbPatFname.Text ||
patientResult_DS.LAST_NAME == tbPatLname.Text ||
patientResult_DS.ADDRESS1 == tbPatAddr.Text ||
patientResult_DS.CITY == tbPatCity.Text ||
patientResult_DS.PHONE == tbPatPhone.Text ||
patientResult_DS.SSN == tbDocZip.Text)
select new { patientResult_DS.FIRST_NAME, patientResult_DS.LAST_NAME, patientResult_DS.DATE_OF_BIRTH, patientResult_DS.ADDRESS1, patientResult_DS.SEX, patientResult_DS.PHONE };
this.UltraWebGridPatSrch.DataSource = patientResult_DS;
As Stated about do this:
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord. patattndby docdata.MP I == patientdata.MPI &&
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).ToList();
patientResult_DS.AddRange( (IEnumerab le<PATIENT >)PatientR esult);
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord.
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).ToList();
patientResult_DS.AddRange(
ASKER
sree,
I have a new error now on this line.
patientResult_DS.AddRange( (IEnumerab le<PATIENT >)PatientR esult);
Unable to cast object of type 'System.Collections.Generi c.List`1[< >f__Anonym ousType5`1 [RadRelay3 5sp1.PATIE NT]]' to type 'System.Collections.Generi c.IEnumera ble`1[RadR elay35sp1. PATIENT]'.
I have to run...I will be back shortly and check back. Thanks for all your help you are giving me.
I have a new error now on this line.
patientResult_DS.AddRange(
Unable to cast object of type 'System.Collections.Generi
I have to run...I will be back shortly and check back. Thanks for all your help you are giving me.
try:
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord. patattndby docdata.MP I == patientdata.MPI &&
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).ToList();
foreach(var p in patientResult)
PatientResult_DS.Add(patat tndbydocda ta);
this.UltraWebGridPatSrch.D ataSource = patientResult_DS;
List<PATIENT> patientResult_DS = new List<PATIENT>();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord.
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).ToList();
foreach(var p in patientResult)
PatientResult_DS.Add(patat
this.UltraWebGridPatSrch.D
ASKER
sree,
I tried the Add(patattndbydocdata) but it told me it did not exist in the current context. I then tried PatAttndByDocResultRecord and also PatAttndByDocResult. When I used those variables I got the error
Error 2 The best overloaded method match for 'System.Collections.Generi c.List<Rad tRelay35sp 1.PATIENT> .Add(RadRe lay35sp1.P ATIENT)' has some invalid arguments
I tried the Add(patattndbydocdata) but it told me it did not exist in the current context. I then tried PatAttndByDocResultRecord and also PatAttndByDocResult. When I used those variables I got the error
Error 2 The best overloaded method match for 'System.Collections.Generi
Sorry should be:
foreach(var p in patientResult)
PatientResult_DS.Add(PatAt tndByDocRe sult.patat tndbydocda ta);
foreach(var p in patientResult)
PatientResult_DS.Add(PatAt
ASKER
sree,
PatAttndByDocResult did not have a patattndbydocdata as a choice, It only had Provider as a choice. I tried that and it gave me
Error 2 The best overloaded method match for 'System.Collections.Generi c.List<Cha rtRelay35s p1.PATIENT >.Add(RadR elay35sp1. PATIENT)' has some invalid arguments
PatAttndByDocResult did not have a patattndbydocdata as a choice, It only had Provider as a choice. I tried that and it gave me
Error 2 The best overloaded method match for 'System.Collections.Generi
ASKER
sree,
Here is the code you asked me to use and then the exact error message
foreach (var p in PatientResult)
patientResult_DS.Add(PatAt tndByDocRe sult.patat tndbydocda ta);
Error 2 'System.Linq.IQueryable<An onymousTyp e#1>' does not contain a definition for 'patattndbydocdata' and no extension method 'patattndbydocdata' accepting a first argument of type 'System.Linq.IQueryable<An onymousTyp e#1>' could be found (are you missing a using directive or an assembly reference?)
Here is the code you asked me to use and then the exact error message
foreach (var p in PatientResult)
patientResult_DS.Add(PatAt
Error 2 'System.Linq.IQueryable<An
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it. Your AWESOME!!!!!
Thanks
Kenny
Thanks
Kenny
do something as :
else
{
PATIENT patientResult_DS = new PATIENT();
foreach (var PatAttndByDocResultRecord in PatAttndByDocResult)
{
var PatientResult = (from patientdata in dcRadRelay.PATIENTs
where PatAttndByDocResultRecord.
(patientdata.FIRST_NAME == tbPatFname.Text ||
patientdata.LAST_NAME == tbPatLname.Text ||
patientdata.ADDRESS1 == tbPatAddr.Text ||
patientdata.CITY == tbPatCity.Text ||
patientdata.PHONE == tbPatPhone.Text ||
patientdata.SSN == tbDocZip.Text)
select new { patientdata }).Single(); // also check for .First() / FirstOrDefault() -- suits.
patientResult_DS.FIRST_NAM
patientResult_DS.LAST_NAME
patientResult_DS.DATE_OF_B
patientResult_DS.ADDRESS1 = patientdata.ADDRESS1;
patientResult_DS.SEX = patientdata.SEX:
this.UltraWebGridPatSrch.D
In case if result is Multiple Records: you can use : List
eg: LIst<PATIENT> patientResult_DS = new List<PATIENT>();