Reputation: 7239
I am using Visual Studio 2010, ASP.NET MVC 3, with a SQL 2008 R2 Database.
This, I believe is a simple task but I don't exactly know how to word it. This is the current code I have.
public static IEnumerable GetAPCBundledCodesData(string CPTCode)
{
var result = (from item in new VADataContext().CPT2MODs
where item.CPT == CPTCode
select new { item.MOD }).ToList();
return result;
}
This pulls a list of items in the CPTSMODs table and returns it. What I would like to do is take that result and get another list with matches to every item with the same value in another table called Modifiers with the same field MOD. I'm not sure how to do this part.
I hope I've explained this well enough. Let me know if there is any clarification needed.
I've continued to look for an answer and found that my problem is I can have two DataContext in on statement. I'm not even sure if this would work if it wasn't two DataContext but I tried doing this:
public static IEnumerable GetAPCBundledCodesData(string CPTCode)
{
var result = (from mod in new VADataContext().MODIFIERs
join cpt2mod in new VADataContext().CPT2MODs on mod.MOD equals cpt2mod.MOD
where cpt2mod.CPT == CPTCode
select new { mod.MOD, mod.SHORT }).ToList();
return result;
}
This resulted in an error saying I can't pull data from two different sources. Is there a different way to handle it?
Here is some sample data to show what is going on here.
Table CPT2MOD
CPT** MOD**
31624 TC
31624 A
31624 DC
99213 B
99213 T
00100 AS
Table MODIFIERs
MOD** SHORT**
TC TC Desc
A A Desc
DC DC Desc
B B Desc
T T Desc
AS AS Desc
A CPT Code will be passed to GetAPCBundledCodesData (ex 99213) and it will look for all MOD values associated with it from the CPT2MOD table (B, T). Then it will return those MOD from the MODIFIERs table along with the description. (B, B Desc; T, T Desc).
I hope you can see what I am asking better now.
Solution
public static IEnumerable GetAPCBundledCodesData(string CPTCode)
{
using (var dc = new VADataContext()){
var result = (from a in dc.CPT2MODs
where a.CPT == CPTCode
join b in dc.MODIFIERs on a.MOD equals b.MOD
select new { b.MOD, b.SHORT }).ToList();
return result;
}
}
Upvotes: 2
Views: 5513
Reputation: 42363
Use only one ObjectContext - and use a using
(very important):
using(var dc = new VADataContext()){
var result = (from mod in dc.MODIFIERs
join cpt2mod in dc.CPT2MODs on mod.MOD equals cpt2mod.MOD
where cpt2mod.CPT == CPTCode
select new { mod.MOD, mod.SHORT }).ToList();
return result;
}
As an aside - entity framework typically doesn't call the classes it generates DataContext
- that's a Linq to SQL thing - are you sure that you're actually using entity framework? It doesn't actually have any bearing on the fix, it's just a request for clarification.
Upvotes: 1