0

My linq query is:

 var query1 = (from grp in ctnx.tblGroupDatas
                     group grp by grp.MeterID_FK into g
                     let maxId = g.Max(gId => gId.GroupDataID)
                     select new { metId = g.Key, maxId });
 query2 = (from met in ctnx.tblMet
                      from mod in ctnx.tblMod.Where(mo => mo.ModID == met.Mod_FK).DefaultIfEmpty()
                      from grp in ctnx.tblGroupDatas.Where(gr => gr.Met_FK == met.MetID)
                      from group1 in db.tblMetRelateGroups.Where(x => x.Met_FK == met.MetID)

                      from q1 in query1.Where(q => q.metId == met.MetID && grp.GroupDataID == q.maxId)

                      where (group1.GroupMetID_FK == groupID)

                      select new
                      {
                          met.MetID,
                          mod.ModSerial,
                          met.MetSerial,
                          met.MetWaterSharingNo,
                          met.MetPowerSharingNo,
                          grp.GroupDate,
                          grp.GroupDataID
                      });
 var gridobisdata = query2.OrderByDescending(m => m.GroupDate);

but this show error:

The specified LINQ expression contains references to queries that are associated with different contexts.

HadiRj
  • 1,015
  • 3
  • 21
  • 41
mahdis dezfouli
  • 173
  • 3
  • 19
  • You'll have to perform two database queries: – Sajeetharan Dec 26 '15 at 07:31
  • 2
    What is `db` in `from group1 in db.tblMetRelateGroups` You cannot couple queries from two different contexts in a single linq2sql unless it is a in memory operation. – Shaunak D Dec 26 '15 at 07:34
  • @Shaunak D db is "MetControl.Models.DomainModels.MetControlDBEntities db = new MetControlDBEntities();" – mahdis dezfouli Dec 26 '15 at 07:37
  • @ShaunakD it work when i remove " where (group1.GroupMetID_FK == groupID)" why? – mahdis dezfouli Dec 26 '15 at 07:39
  • 2
    Linq must only ever join with data from the same context. IE, Database, DataFile, MemoryTable, Collections, etc... If you try and cross over those boundaries and join them together, then you get the error you're seeing. To resolve that you must copy the data from one context into the others. For example, you can query the database table down into a local collection in memory and then join to that new collection. or vise-versa. – Moon Dec 26 '15 at 07:49
  • @Don I don't Know how to use memory. Please show sample. – mahdis dezfouli Dec 26 '15 at 07:54
  • 1
    Take a look at these two answers: http://stackoverflow.com/a/26169161/2549384 http://stackoverflow.com/a/7339159/2549384 Usually something like `.ToArray();` or `.ToList();` Those basically copy the results into an in memory collection from which you can join to other in memory collections. However if you're connecting to two different databases, then you might be better off moving the data into one. I'm not sure of the sources you're connecting to on every one of your 'tables' – Moon Dec 26 '15 at 07:59
  • @Don Thanks for your help. I find error. – mahdis dezfouli Dec 26 '15 at 08:03
  • Great. If my comments helped, let me know and I will build them into an answer which you can mark as 'accepted' Otherwise, good luck and happy holidays – Moon Dec 26 '15 at 08:04
  • @Don I don't Realize I use tow context different context. In fact i want use same context. But i want to learn how to use two different context. Tanks and happy holidays. – mahdis dezfouli Dec 26 '15 at 08:13

0 Answers0