I have the following 2 tables.
Table1:
Number Table2ID Count
====== ======== =====
1 1 3
1 2 5
1 4 2
1 5 4
2 1 6
2 3 2
2 2 4
2 5 3
Table2:
ID Code Sequence
== ==== ========
1 AA 1
2 BB 2
3 CCC 3
4 D 4
5 EE 5
Using these tables, I want to get the following result:
Number Codes
====== =====
1 AA, BB, D, EE
2 AA, BB, CCC, EE
For this purpose, I wrote the following query (according to this answer):
from tempResult in (from t1 in Table1
join t2 in Table2
on t1.Table2ID equals t2.ID
select new
{
Number = t1.Number,
Code = t2.Code,
Sequence = t2.Sequence
})
group tempResult by tempResult.Number into groupedTempResult
select new
{
Number = groupedTempResult.Key,
Codes = string.Join(", ", groupedTempResult.OrderBy(x => x.Sequence).Select(x => x.Code))
}
Upon executing this query, I faced the following exception (as mentioned in the comment of that answer):
LINQ to Entities does not recognize the method 'System.String Join(System.String, System.String[])' method, and this method cannot be translated into a store expression.
To fix this exception, I made the following correction according to this answer:
from tempResult in (from t1 in Table1
join t2 in Table2
on t1.Table2ID equals t2.ID
select new
{
Number = t1.Number,
Code = t2.Code,
Sequence = t2.Sequence
}).AsEnumerable()
group tempResult by tempResult.Number into groupedTempResult
select new
{
Number = groupedTempResult.Key,
Codes = string.Join(", ", groupedTempResult.OrderBy(x => x.Sequence).Select(x => x.Code))
}
After this modification, I started getting the following exception while executing the query:
Error: Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.
To understand in which select clause I am having problem, I declared a class for each of the select clause - for this reason the query got modified to this:
from tempResult in (from t1 in Table1
join t2 in Table2
on t1.Table2ID equals t2.ID
select new TempResult
{
Number = t1.Number,
Code = t2.Code,
Sequence = t2.Sequence
}).AsEnumerable() // converted the result as enumerable
group tempResult by tempResult.Number into groupedTempResult
select new Result
{
Number = groupedTempResult.Key,
Codes = string.Join(", ", groupedTempResult.OrderBy(x => x.Sequence).Select(x => x.Code))
}
After this modification, I got the following exception:
Error: Unable to create a constant value of type 'Namespace.Name.Result'. Only primitive types or enumeration types are supported in this context.
So from my understanding, the last select clause is where the exception is happening.
I tried to follow the answers of the following 1, 2 questions as much as I could - resulting a new version of the query.
from tempResult in (from t1 in Table1
join t2 in Table2
on t1.Table2ID equals t2.ID
select new TempResult
{
Number = t1.Number,
Code = t2.Code,
Sequence = t2.Sequence
}).AsEnumerable()
.GroupBy(x => x.Number)
.Select(x => new Result { Number = x.Key, Codes = string.Join(", ", x.OrderBy(y => y.Sequence).Select(y => y.Code)) })
select tempResult
This did not solve the exception mentioned earlier.
At this point, I am very much out of ideas about how can I get my desired result.
Any help regarding this issue is highly appreciated.
A point to note that I have to join the result of this query / operation with another query. So breaking this query to multiple statements / operations is not what I am looking for.
Edit: Let me try to clarify on how I am trying to use this query actually.
from otherResult1 in resultFromAnotherQuery1
join result in (from tempResult in (from t1 in Table1
join t2 in Table2
on t1.Table2ID equals t2.ID
select new TempResult
{
Number = t1.Number,
Code = t2.Code,
Sequence = t2.Sequence
}).AsEnumerable()
.GroupBy(x => x.Number)
.Select(x => new Result { Number = x.Key, Codes = string.Join(", ", x.OrderBy(y => y.Sequence).Select(y => y.Code)) })
select tempResult).ToList()
on otherResult1.Number equals result.Number
join otherResult2 in resultfromAnotherQuery2
on otherResult1.ColumnA equals otherResult2.ColumnB
.....
select new FinalResult
{
.......
Codes = result.Codes,
.......
}
If I skip this joining with result
and otherResult1
and ignore populating the Codes
field in FinalResult
class - just work with otherResult1
and otherResult2
, there is no problem to execute the query. But when I try to do this join, I face the exception mentioned in the question.