在 Linq 查询中连接 Linq 查询的结果

本文关键字:查询 Linq 结果 连接 | 更新日期: 2023-09-27 18:34:31

我遇到的问题是,除了查询中执行串联的部分出现问题外,查询工作正常,连接查询在查询之外工作,但当我放入它时将不起作用,并在代码下最后出现错误。

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 {System.NotSupportedException}

在 Linq 查询中连接 Linq 查询的结果

问题恰恰在于错误告诉您的内容 - String.Concat无法转换为SQL查询。因此,将查询分为两部分 - 一部分查询数据库,另一部分在执行 concat 之前将数据放入内存。

    var listQuery =
        from x in context.Contacts
        where !x.DeleteFlag && !x.EmptyFlag
        orderby x.FirstName
        select new
        {
            Contact = x,
            Phone =
                context.EContacts.Where(e => e.id == x.PrimaryPhoneid)
                .Select(e => e.Title).FirstOrDefault(),
            Email =
                context.EContacts.Where(e => e.id == x.PrimaryEmailid)
                .Select(e => e.Title).FirstOrDefault(),
            Account =
                context.Accounts.Where(e => e.id == x.Parentid)
                .Select(e => e.AccountName).FirstOrDefault(),
            tags =
                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,                
        };
    var list =
        from x in listQuery.ToArray()
        select new models.Contacts.list()
        {
            Contact = x.Contact,
            Phone = x.Phone,
            Email = x.Email,
            Account = x.Account,
            tags = String.Concat(x.tags.ToArray()),
        };

你为什么用string.Contat?只是做总结。

tag = mainQuery.AccountName + (childQuery.ToArray().Aggregate(x,y)=> x + "," + y)

如果上面的方法没有帮助,我建议您进行两个单独的查询。第一个将获取联系人,第二个将计算哈希标签。然后只需合并它。它将提高性能。

在我看来,最好的方法是将本机 T-SQL 与实体框架一起使用。

class MyDataRow
{
    public int Id {get;set;}
    public double FieldB {get;set;}
    public string FieldC {get;set;}
}
string queryText = @"
SELECT 
    t1.Id, 
    t1.FieldB, 
    (SELECT hashTag FROM table_2 WHERE contactId = t1.Id) AS FieldC
FROM table_1 AS t1
";
// EF will map properties automatically and you do not have to write and configure stored procedure...
List<MyDataRow> rows = context.ExecuteStoreQuery<MyDataRow>(queryText).ToList();