我遇到的问题是,查询工作正常,除了查询部分的问题连接,连接查询在查询外工作,但是当我放入其中时它将无法工作并在最后得到错误码 .
var list = (from x in context.Contacts
where !x.DeleteFlag && !x.EmptyFlag
select new models.Contacts.list
{
// CONTACT
Contact = x,
// CONTACT'S PHONE
Phone =
context.EContacts.Where(e => e.id == x.PrimaryPhoneid)
.Select(e => e.Title).FirstOrDefault(),
// CONTACT'S EMAIL
Email =
context.EContacts.Where(e => e.id == x.PrimaryEmailid)
.Select(e => e.Title).FirstOrDefault(),
// CONTACT'S ACCOUNT
Account =
context.Accounts.Where(e => e.id == x.Parentid)
.Select(e => e.AccountName).FirstOrDefault(),
// Problem Is Here With This Query
tag = string.Concat((from HE in context.HashTagEntities
join t in context.Accounts on HE.ParentEntityid equals t.id
where HE.ParentEntityId == 3 &&
t.AccountName == context.Accounts.Where(e => e.id == x.Parentid).Select(e => e.AccountName)
.FirstOrDefault()
from tag in context.HashTags
where HE.HashTagid == tag.id
select tag.HashTagText).ToArray()),
}).OrderBy(o => o.Contact.FirstName);
错误:
{“LINQ to Entities无法识别方法'System.String Concat(System.String [])'方法,并且此方法无法转换为存储表达式 . ”} System.SystemException
2 回答
问题正是错误告诉你的 -
String.Concat
无法转换为SQL查询 . 因此,将查询分为两部分 - 一部分查询数据库,另一部分在执行concat之前将数据带入内存 .你为什么用
string.Contat
?做个总结 .如果上面的方法没有帮助,我建议你做两个单独的查询 . 首先是获取联系人,第二个是计算HashTags . 然后合并它 . 它将提高性能 .
我认为最好的方法是使用本机T-SQL和实体框架 .