0

I am wondering if anyone can help me, the issue I have is that I cant return the results in a concatenate string using Linq. The query works fine the issue is how to concatenate the results as string, string so forth as there can be many hash tags

var contacttag = (from HE in HashTagEntities
join t in Accounts on HE.Parentid equals t.id
where HE.ParentId == 3 &&
t.AccountName == "Test"
from tag in HashTags
where HE.HashTagid == tag.HOCODE
select new { tag.HashTagText }).Select(x => x.HashTagText.ToString());

If anyone could help I would be grateful I am getting a error below:

"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."

1
  • I think if you cast the query (from HE in ... tag.HashTagText }) to IEnumerable<T> (where T is the type of HashTagText), it should work. That ought to avoid going through the Entities extension methods. If that doesn't work, you might have to use ToList() before the Select() to get out of the Entities handling of the query. Commented Dec 16, 2014 at 19:35

1 Answer 1

2

You are already selecting a collection of strings with select new { tag.HashTagText }, so there is no need to select them again or call .ToString() on them.

You should be able to simply use string.Join():

var contacttag = from HE in HashTagEntities
                 join t in Accounts on HE.Parentid equals t.id
                 where HE.ParentId == 3 &&
                 t.AccountName == "Test"
                 from tag in HashTags
                 where HE.HashTagid == tag.HOCODE
                 select tag.HashTagText;

var tags = string.Concat(contacttag);

or in .NET 3.5:

var tags = string.Concat(contacttag.ToArray());

I can't see any reason why one would insist on doing this in a single statement, but it can be done:

var tags = string.Concat((from HE in HashTagEntities
                 join t in Accounts on HE.Parentid equals t.id
                 where HE.ParentId == 3 &&
                 t.AccountName == "Test"
                 from tag in HashTags
                 where HE.HashTagid == tag.HOCODE
                 select tag.HashTagText).ToArray());
Sign up to request clarification or add additional context in comments.

12 Comments

Could it be done in the one query, I have tried var tags = string.Join("", THE FULL QUERY TO ARRAY ); and I got Linq does not recognize string.Join
@BillyJoe - You mean in one line? Personally i think it is more readable if you first define the query, and then concatenate the resulting strings.
I reason why I asked was because, this one part of a query which is filling a list for each customer of many customers. I wanted to keep the questain straight forward and I tried not to over complicate it.
@BillyJoe As Martin says, I don't know why you would insist on doing this in one line, but it can be done. Please see my update.
The error I am getting is Message "LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' method, and this method cannot be translated into a store expression." string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.