I have a stored procedure that returns all the columns from a table (InstitutionEntityIP) that maps to the DBML object of the same name. It will return n number of records. However, the codebehind defaults to ISingleResult. I would like to have it return an IEnumerable.
I'm getting an error message 'The query results cannot be enumerated more than once.'
My code looks like this:
//ContentRepository.cs
private readonly IGIGlobalDataContext db;
// ....
public IEnumerable<InstitutionEntityIP> GetInstitutionEntityIPs(long fromIp, long toIp)
{
return
from ip in db.InstitutionEntityIPs
where
(
(fromIp >= ip.FromIPInteger && fromIp <= ip.ToIPInteger) || //From IP Address parameter is the same or encompassed
(toIp >= ip.FromIPInteger && toIp <= ip.ToIPInteger) || //To IP Address parameter is the same or encompassed
(fromIp <= ip.FromIPInteger && toIp >= ip.FromIPInteger) || //Any From IPs in the DB between the parameters
(fromIp <= ip.ToIPInteger && toIp >= ip.ToIPInteger) //Any To IPs in the DB between the parameters
) &&
ip.IsActive &&
ip.InstitutionEntity.IsActive &&
Convert.ToDateTime((ip.InstitutionEntity.ExpirationDate ?? (DateTime?)DateTime.Now)) >= DateTime.Now &&
ip.InstitutionEntity.Institution.IsActive &&
Convert.ToDateTime((ip.InstitutionEntity.Institution.ExpirationDate ?? (DateTime?)DateTime.Now)) >= DateTime.Now
select ip;
}
public IEnumerable<InstitutionEntityIP> GetInstitutionEntityIPs(string fromIp, string toIp)
{
// SearchForConflictingIPs is the method name created when I drop my
// stored procedure (also called SearchForConflictingIPs) on the DBML layout
var ips = db.SearchForConflictingIPs(fromIp, toIp); // returns ISingleResult<InstitutionEntityIP>
// VVVVVVVVV
return ips.ToList(); // <-- Needed to specify the .ToList() here <--
// ^^^^^^^^^
}
// I actually call the method from another class
public static IEnumerable<IGIGlobal_DAL.InstitutionEntityIP> ConflictingIPs(string start, string end)
{
var cr = new ContentRepository();
return cr.GetInstitutionEntityIPs(start, end);
}
public RangeValidationResult Validate(bool isUpdate = false, int institutionEntityIPID = 0)
{
var conflicts = ConflictingIPs(Start.ToString(), End.ToString());
if (conflicts.Any()) // <---- 'The query results cannot be enumerated more than once.'
{
var conflictingEntities = string.Join(", ", conflicts.Select(c => c.InstitutionEntity.InstitutionEntity1).Distinct());
}
}
If you need any more information from me please comment.