【问题标题】:How to convert a group_concat, join, and union query into linq lambda?如何将 group_concat、join 和 union 查询转换为 linq lambda?
【发布时间】:2015-10-14 15:46:39
【问题描述】:

我对 linq-lambda 很陌生。我有一个 MySQL 查询,它将两个表中的项目名称合并在一起。然后拉出另一列项目所属的专业。

我的工作查询是这样的:

 Select
    p.title,
    GROUP_CONCAT(spec.categoryname) genre 
 From
    (SELECT FullName AS title, TypeId as typeid, Id as id FROM programs
        UNION
    SELECT FullName, TypeId, Id FROM products) 
        AS p
Inner join  specialtymembers mem  on (ItemType = p.typeid AND ItemId = p.id)
Inner join specialties spec on mem.Category = spec.id
 GROUP BY p.title
 ORDER BY p.title

现在我的问题是......我必须以某种方式将其转换为 linq lambda。我的尝试是这样的:

var allitems =
            _programContentService.Products.Select(r => r.FullName)
                .Union(_programContentService.Programs.Select(q => q.FullName))
                .Join(_programContentService.SpecialtyMembers, z => z.ItemType, q => q.ItemType,
                    (z, q) => new {z, q})
                .Join(_programContentService.Specialties, x => x.Id, z => z.Category,
                    (x, z) => new {x,z})
                    .Select(@t => new SelectListItem { Name = q.FillName.ToString(), Genre = "SOME HOW HAVE TO USE A LOOPING FEATURE??"  });

更新

var allitems = _programContentService
            .ProgramProductViews
            .Select(x => new {x.FullName, x.TypeId, x.Id})
            .Join(
                _programContentService.SpecialtyMembers,
                type => new {type.TypeId, type.Id},
                member => new {TypeId = member.ItemType, Id = member.ItemId},
                (type, member) => new {type.FullName, member.Category})
            .Join(
                _programContentService.Specialties,
                q => q.Category,
                specialty => specialty.Id,
                (q, specialty) => new { q.FullName, specialty.SpecialtyName })
            .GroupBy(x=>x.FullName)
            .AsEnumerable()
            .Select(x => new SelectListItem
                {
                    Value = x.Key,
                     Text = String.Join(",", x.Select(q=>q.SpecialtyName))
                });  

我感觉我很亲近……

【问题讨论】:

    标签: c# mysql asp.net-mvc linq lambda


    【解决方案1】:

    我认为这会让你得到你想要的。它未经测试,如果您有问题,请告诉我,我会解决的。

    var allitems =
        _programContentService
        .Products
        .Select(x => new { x.FullName, x.TypeId, x.Id })
        .Union(
            _programContentService
            .Programs
            .Select(x => new { x.FullName, x.TypeId, x.Id }))
        .Join(
            _programContentService.SpecialtyMembers,
            type => new { type.TypeId, type.Id },
            member => new { TypeId = member.ItemType, Id = member.ItemId },
            (type, member) => new { type.FullName, member.Category })
        .Join(
            _programContentService.Specialties,
            x => x.Category,
            specialty => specialty.Id,
            (x, specialty) => new { x.FullName, specialty.CategoryName })
        .GroupBy(x => x.FullName)
        .AsEnumerable()
        .Select(x => new SelectListItem 
            { 
                Value = x.Key, 
                Text = String.Join(",", x.Select(y => y.CategoryName))
            });
    

    【讨论】:

    • 不错!!!!我测试了它。一切看起来都不错,除了我遇到了一些错误。在第二次加入时,我收到错误消息:“无法从用法中推断出类型参数。尝试明确指定类型参数”并且在 .GroupBy 上,它无法解析符号“FullName”。然后在新的选择列表项中它无法解析“键”。
    • 如果我删除第二个 .Join 它没有错误。所以第二次加入会发生很多事情。嗯....感谢您的帮助!
    • @NeoSketo - 我之前已经看到,当被连接的属性是两种不同的类型。 SpecialtyMembers.CategorySpecialties.Id 是什么类型?任一个都可以为空吗?
    • 啊!你说的对! SpecialtyMembers.Category 在应该是 int 时被设置为字符串。我改变了它,一切都很好!谢谢!谢谢!
    • - 嘿!看起来最后的部分名称和流派只能是文本和值。所以我把它改成了:Value = x.Key, Text = String,Join(",", x)。但主要问题是 String.Join(",",x)。它给了我这个错误: InnerException {"The given expression 'new f__AnonymousTypeb`2(FullName = [x].FullName, SpecialtyName = [specialty].SpecialtyName)' does not contain the searched expression '[x]' in a具有成员分配或 MemberBindingExpression 的嵌套 NewExpression。\r\n参数名称:fullExpression"} System.Exception {System.ArgumentException}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2020-06-01
    • 2018-09-23
    • 2021-05-30
    相关资源
    最近更新 更多