c# - visual - mssql to linq




在LINQ中分組 (5)

假設我們有類似的課程

class Person { 
    internal int PersonID; 
    internal string car  ; 
}

現在我有這個類的列表: List<Person> persons;

現在這個列表可以有多個具有相同PersonID的實例,例如:

persons[0] = new Person { PersonID = 1, car = "Ferrari" }; 
persons[1] = new Person { PersonID = 1, car = "BMW"     }; 
persons[2] = new Person { PersonID = 2, car = "Audi"    }; 

有沒有一種方法可以通過personID進行分組並獲得他擁有的所有車輛的列表?

例如,預期的結果是

class Result { 
   int PersonID;
   List<string> cars; 
}

所以分組後,我會得到:

results[0].PersonID = 1; 
List<string> cars = results[0].cars; 

result[1].PersonID = 2; 
List<string> cars = result[1].cars;

從我迄今所做的事情來看:

var results = from p in persons
              group p by p.PersonID into g
              select new { PersonID = g.Key, // this is where I am not sure what to do

有人能指點我正確的方向嗎?


你也可以試試這個。

var results= persons.GroupBy(n => new { n.PersonId, n.car})
                .Select(g => new {
                               g.Key.PersonId,
                               g.Key.car)}).ToList();

嘗試

persons.GroupBy(x => x.PersonId).Select(x => x)

要么

檢查是否有人在你的名單中重複嘗試

persons.GroupBy(x => x.PersonId).Select(x => x.Count() > 1).Any(x => x)

我用查詢語法和方法語法創建了一個工作代碼示例。 我希望它可以幫助其他人:)

您也可以在這裡運行.Net小提琴上的代碼:

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

class Person
{ 
    public int PersonId; 
    public string car  ; 
}

class Result
{ 
   public int PersonId;
   public List<string> Cars; 
}

public class Program
{
    public static void Main()
    {
        List<Person> persons = new List<Person>()
        {
            new Person { PersonId = 1, car = "Ferrari" },
            new Person { PersonId = 1, car = "BMW" },
            new Person { PersonId = 2, car = "Audi"}
        };

        //With Query Syntax

        List<Result> results1 = (
            from p in persons
            group p by p.PersonId into g
            select new Result()
                {
                    PersonId = g.Key, 
                    Cars = g.Select(c => c.car).ToList()
                }
            ).ToList();

        foreach (Result item in results1)
        {
            Console.WriteLine(item.PersonId);
            foreach(string car in item.Cars)
            {
                Console.WriteLine(car);
            }
        }

        Console.WriteLine("-----------");

        //Method Syntax

        List<Result> results2 = persons
            .GroupBy(p => p.PersonId, 
                     (k, c) => new Result()
                             {
                                 PersonId = k,
                                 Cars = c.Select(cs => cs.car).ToList()
                             }
                    ).ToList();

        foreach (Result item in results2)
        {
            Console.WriteLine(item.PersonId);
            foreach(string car in item.Cars)
            {
                Console.WriteLine(car);
            }
        }
    }
}

結果如下:

1
Ferrari
BMW
2
Audi
-----------
1
Ferrari
BMW
2
Audi


絕對 - 你基本上想要:

var results = from p in persons
              group p.car by p.PersonId into g
              select new { PersonId = g.Key, Cars = g.ToList() };

或者作為非查詢表達​​式:

var results = persons.GroupBy(
    p => p.PersonId, 
    p => p.car,
    (key, g) => new { PersonId = key, Cars = g.ToList() });

基本上,該組的內容(當作為IEnumerable<T>查看時)是給定鍵上的投影中的任何值的序列(本例中為p.car )。

有關GroupBy如何工作的更多信息,請參閱關於該主題的我的Edulinq文章

(我在上面將PersonID重命名為PersonId ,以遵循.NET命名約定 。)

或者,您可以使用Lookup

var carsByPersonId = persons.ToLookup(p => p.PersonId, p => p.car);

然後,您可以非常輕鬆地為每個人獲取車輛:

// This will be an empty sequence for any personId not in the lookup
var carsForPerson = carsByPersonId[personId];

var results = from p in persons
              group p by p.PersonID into g
              select new { PersonID = g.Key, Cars = g.Select(m => m.car) };




group-by