动态搜索和Linq返回逗号分隔的字符串

时间:2016-07-01 08:46:09

标签: c# linq search dynamic

我使用LINQ将多行连接成单行。我有问题在动态搜索页面使用。

User  Hobby
--------------
Bob   Football 
Bob   Golf 
Bob   Basketball
Sue   Sleeping 
Sue   Drinking

User  Hobby
--------------
Bob   Football, Golf, Tennis 
Sue   Sleeping, Drinking

var users = new[]
{
    new { User="Bob", Hobby="Football" },
    new { User="Bob", Hobby="Golf" },
    new { User="Bob", Hobby="Tennis" },
    new { User="Sue", Hobby="Sleeping" },
    new { User="Sue", Hobby="Drinking" },
};

 var userList = users.ToList();

 var result1 = (from user in users
                group user by user.User into groupedUserList
                select new { 
                    user = groupedUserList.Key, 
                    hobby = groupedUserList.Select(x => x.Hobby).Aggregate((a, b) => (a + ", " + b))
                });

 var result2 = (from groupeduser in ug
 select new{ groupeduser.user, hobby =string.Join(",", groupeduser.hobby)});

如果我添加带有爱好的搜索字段

  

with result1 = result1.Where(t => t.Hobby.Contains(“ball”));

会抛出错误--->聚合不受支持

  

with result2 = result2.Where(t => t.Hobby.Contains(“ball”));

它会抛出System.String Join(System.String,System.String [])不支持翻译成SQL

5 个答案:

答案 0 :(得分:1)

仅在Linq-To-Objects支持,因此您可以使用AsEnumerableToList

var userGroups = from user in users
                 group user by user.User into groupedUserList
                 select new { 
                     user = groupedUserList.Key, 
                     userHobbies = groupedUserList.Select(x => x.Hobby)
                };

foreach(var grp in userGroups.AsEnumerable())
    Console.WriteLine("user:{0} hobbies:{1}"
        , grp.user
        , String.Join(",", grp.userHobbies));  

如果要过滤结果,则应在将其加载到内存之前执行此操作(例如ToList)。因此,首先使用Where

var usersGroupsWithBallHobbies = userGroups
    .Where(g => g.userHobbies.Any(hobby => hobby.Contains("ball")));
foreach(var grp in usersGroupsWithBallHobbies.AsEnumerable())
    ...

Any + String.Contains也适用于Linq-To-Entities(> = .NET 4)

答案 1 :(得分:0)

var result = from user in users
         group user by user.User into groupedUserList
         select new { User = groupedUserList.Key, 
                      Hobby = string.Join(", ", 
                          groupedUserList.Select(item => item.Hobby).ToList()) };

答案 2 :(得分:0)

var groupedByUser = users.GroupBy(u => u.User)
                         .Select(g => new { User = g.Key, Hobbies = String.Join(", ", g.Select(u => u.Hobby)) })
                         .ToList();

答案 3 :(得分:0)

您应该执行" SQL支持的"首先请求使用您的where子句获取按业余爱好分组的数据,如下所示:

var result1 = (from user in users
               where user.Hobby.Contains("Ball")
               group user by user.User);

然后使用ToList()将数据加载到内存中并执行业余爱好的连接,如下所示:

var result2 = result1
    .ToList()
    .Select(x => new {
        User = x.Key,
        Hobbies = string.Join(",", x.Select(pair => pair.Hobby))
    });

总结一下:

  • result1包含 Linq-to-Sql 查询。
  • result2包含 In-Memory 查询,这是您的最终结果。

答案 4 :(得分:0)

var hobbySearchKey = "ball";

users
    .GroupBy(_ => _.User, _ => _.Hobby) // group users with hobbies
    .Where(_ => _.Any(hobby => hobby.Contains(hobbySearchKey))) // filter only users whose hobbies contain a search key
    .Select(_ => new { User = _.Key, Hobbies = string.Join(",", _)}); // concatenate hobbies

Any LINQ to SQL

上的MSDN

string.Contains

上的MSDN博客