简体   繁体   English

连接Linq查询内的Linq查询结果

[英]Concatenate the results of a Linq query within a Linq query

The issue that I have is, the query works okay apart from problem with the part of the query which does the concatenation, the concatenation query works outside the query but when I put inside it will not work and get the error at the end under the code. 我遇到的问题是,该查询可以正常工作,而与进行串联的查询部分无关,串联查询可以在查询外部进行,但是当我将其放入其中时,它将无法正常工作并在码。

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);

error: 错误:

{"LINQ to Entities does not recognize the method 'System.String Concat(System.String[])' method, and this method cannot be translated into a store expression."} System.SystemException {System.NotSupportedException} {“ LINQ to Entities无法识别方法'System.String Concat(System.String [])'方法,并且该方法无法转换为商店表达式。”} System.SystemException {System.NotSupportedException}

The problem is exactly what the error is telling you - String.Concat cannot be translated into a SQL query. 问题恰恰是错误告诉您的内容String.Concat无法转换为SQL查询。 So break the query into two parts - one that queries the database and the second that brings the data into memory before doing the 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()),
        };

Why do you use string.Contat ? 为什么使用string.Contat Just do summ. 做总结。

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

If method above didn't help I suggest you to make two separate queries. 如果上述方法无济于事,建议您进行两个单独的查询。 First will fetch contacts and the second will compute HashTags. 第一个将获取联系人,第二个将计算HashTag。 Then just merge it. 然后将其合并。 It will improve performance. 它将提高性能。

And the best way in my opinion is to use native T-SQL with entity framework. 我认为最好的方法是将本机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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM