我想知道是否有人可以帮助我,我遇到的问题是我无法使用 Linq 以连接字符串返回结果。查询工作正常,问题是如何将结果连接为字符串、字符串等,因为可以有很多哈希标签
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());
如果有人可以提供帮助,我将不胜感激,我在下面收到错误:
"LINQ to Entities 无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式。
您已经在选择带有 select new { tag.HashTagText }
的字符串集合,因此无需再次选择它们或对它们调用.ToString()
。
您应该能够简单地使用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);
或在 .NET 3.5 中:
var tags = string.Concat(contacttag.ToArray());
我看不出有什么理由坚持在一个声明中这样做,但可以做到:
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());