我在下面有一個測驗 LINQ 串列
List<test> test=new List<test>();
test.Add(new test("Bakery","[email protected]","Donut"));
test.Add(new test("Bakery","[email protected]","Bagel"));
test.Add(new test("Bakery","[email protected]","Cake"));
test.Add(new test("Produce","[email protected]","Apple"));
test.Add(new test("Dairy","[email protected]","Milk"));
test.Add(new test("Dairy","[email protected]","Yogurt"));
有些部門有超過 1 個專案。如何遍歷串列并向每個部門/電子郵件地址發送 1 封電子郵件和專案。向 [email protected] 發送 1 封電子郵件,其中包含 Donut、Bagel、Cake。發送 1 封電子郵件至 [email protected],正文中包含 Apple。向[email protected] 發送1 封電子郵件,其中包含牛奶、酸奶
我不需要電子郵件部分的幫助,只需要幫助遍歷串列并獲取每個部門的專案。非常感謝!!!
uj5u.com熱心網友回復:
我認為,您可以將分組用于任務。
var groups = test.GroupBy(x => x.Email);
foreach (var group in groups)
{
var email = group.Key; // Exactly the property from GroupBy
var goods = group.Select(x => x.Good); // Donut, Bagel, Cake...
var body = string.Join(", ", goods);
// Now you can send the email with body containing "Donut, Bagel. Cake"
}
group變數的每個實體都包含Key屬性和集合中分組項的test集合。
因此,您可以將group與 之類的方法一起使用,也可以將其Select與foreach運算子一起使用。
uj5u.com熱心網友回復:
List<test> test=new List<test>();
test.Add(new test("Bakery","[email protected]","Donut"));
test.Add(new test("Bakery","[email protected]","Bagel"));
test.Add(new test("Bakery","[email protected]","Cake"));
test.Add(new test("Produce","[email protected]","Apple"));
test.Add(new test("Dairy","[email protected]","Milk"));
test.Add(new test("Dairy","[email protected]","Yogurt"));
foreach (var group in test.GroupBy(x=> x.Department))
{
var department = group.Key;
var email = group.First().Email;
var body = string.Join(", ", group.Select(x => x.Product));
SendMail(email, department, body);
}
uj5u.com熱心網友回復:
如果您想對(專案)進行分組GroupBy,讓我們(按Department和Address)。首先,讓我們準備資料:
var emails = test
.GroupBy(item => (department : item.Department, address : item.Address),
item => item.Item)
.Select(group => (department : group.Key.department,
address : group.Key.address,
body : string.Join(", ", group.Distinct().OrderBy(item => item))));
然后我們準備發送郵件:
foreach (var letter in emails)
SendEmail(letter.address, letter.deparment, letter.body);
uj5u.com熱心網友回復:
你確定所有tests部門Bakery的人都有相同的電子郵件地址嗎?或者可能是(例如,由于輸入錯誤)某些面包店有不同的電子郵件地址?
如果所有 Bakeries 都具有相同的地址,那么您可以使用Enumerable.GroupBy的多載之一來創建具有相同部門的測驗組。然后為每個組獲取該組中第一個測驗的電子郵件(或任何其他,無論如何它們都是相同的)。使用帶引數的多載resultselector來指定結果
class Test
{
public string Department {get; set;}
public string EmailAddress {get; set;}
... // other properties
}
IEnumerable<Test> myTests = ...
var departmentsEmailAddresses = myTests.GroupBy(
// make groups of test that have the same value for Department
test => test.Department,
// parameter resultSelector: for every Department name and all Tests that have
// this value for Department name, make one new object:
(department, testsWithThisValueForDepartment) => new
{
// if desired: remember the department name
Department = department,
// to get the e-mail address, take the first test.
// BTW: all tests in this group will have the same e-mail address
Email = testsWithThisValueForDepartment.Select(test => test.EmailAddress)
.FirstOrDefault(),
})
換句話說:對于所有測驗,創建一組具有相同屬性值的測驗Department。每個小組至少有一個測驗。Department從每個組中,獲取該組中所有測驗的共同值。此外,在該組中進行測驗,并選擇 的值EmailAddress。僅對組中的第一個測驗執行此操作。
結果將是一系列的組合[Department, Email]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522814.html
標籤:C#林克
