简体   繁体   English

如何用两个联接表a和B的列表编写linq查询,然后按a的ID分组,并从B联接行数

[英]How to write linq query with list of two joined tables a and B, then grouped by id of a, and with amount of joined rows from B

How to write Linq query with list of two joined tables A and B, then grouped by id of A, and with amount of joined rows from B 如何用两个联接表A和B的列表编写Linq查询,然后按A的ID分组,并从B联接行数

For example 例如

class A{
   int Id,
   int BId,
   string AName
};

class B{
   int Id,
   string description
};

And to display as follows (after joins of A with B) A.Bid = B.Id 并显示如下(在A与B连接之后)A.Bid = B.Id

A Id |  count(Id) | AName

Frankly, I have to implement following query from PHP to LINQ. 坦白说,我必须实现从PHP到LINQ的以下查询。 All dependencies are written in query below: 所有依赖项都写在下面的查询中:

select("asset.id as Id,asset.AssetBundleID as AssetBundleID,asset.GUID as GUID,asset.Name as Id,asset.Name as Id, asset.DisplayName as DisplayName,asset.Description as Description, asset.Category as Category,asset.TexturesSize as TexturesSize, asset.TexturesFileSize as TexturesFileSize, asset.OthersSize as OthersSize, asset.id as Id, asset.OthersFileSize as OthersFileSize, count(*) as InstancesCount") select(“ asset.id作为Id,asset.AssetBundleID作为AssetBundleID,asset.GUID作为GUID,asset.Name作为Id,asset.Name作为Id,asset.DisplayName作为DisplayName,asset.Description作为Description,asset.Category作为Category ,asset.TexturesSize为TexturesSize,asset.TexturesFileSize为TexturesFileSize,asset.OthersSize为OthersSize,asset.id为Id,asset.OthersFileSize为OthersFileSize,count(*)为InstancesCount“)
->from("asset") ->从(“资产”)
->join('Left Join',"assetinstance"," assetinstance . AssetId = asset . Id ") - >加入( 'LEFT JOIN', “assetinstance”, “ assetinstanceAssetId = assetId ”)
->groupBy("asset.Id") -> groupBy(“ asset.Id”)

Did you mean something like this? 你的意思是这样吗?

using System;
using System.Collections.Generic;
using System.Linq;

public class A 
{
    public int Id {get; set;}
    public int BId {get; set;}
    public string AName {get; set;}
}

public class B{
    public int Id {get; set;}
    public string description{get; set;}
}


public class Program
{
    private static List<A> _aList = new List<A>();  
    private static List<B> _bList = new List<B>();  

    public static void Main()
    {
        Seed();

        var query =
            from b in _bList
            group b by b.Id into g
            join a in _aList on g.FirstOrDefault().Id equals a.BId         
            select new { A_ID = a.Id, Cnt = g.Count(), AName = a.AName};

        Console.WriteLine("A_ID | count(Id) | AName");
        foreach(var row in query) 
        {
            Console.WriteLine(string.Join(" | ", new string[]{row.A_ID.ToString(), row.Cnt.ToString(), row.AName}));
        }
    }

    public static void Seed() 
    {       
        _aList.Add(new A{Id=1, BId=1, AName="A1"});
        _aList.Add(new A{Id=2, BId=1, AName="A2"});
        _aList.Add(new A{Id=3, BId=1, AName="A3"});
        _aList.Add(new A{Id=4, BId=2, AName="A4"});

        _bList.Add(new B{Id=1, description="B1"});
        _bList.Add(new B{Id=2, description="B2"});
    }
}

See my fiddle https://dotnetfiddle.net/NlAX5V 看到我的小提琴https://dotnetfiddle.net/NlAX5V

var result= (from A in ListA
             join B in List B
             on A.ID equals B.ID
             select new {A,B})
             .GroupBy(x=>x.A.Id)
             .Select(x=>new
              {
                ID=x.Key
                count=x.Count(),
                AName=x.Select(z=>z.A.AName)
              }).ToList();

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

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