簡體   English   中英

如何使用 ToList() 打印多個列表項?

[英]How to use ToList() to print multiple list items?

從列表中選擇字段后嘗試打印列表中的項目時,我收到一條錯誤消息,提示“無法從列表轉換為項目”。 一些查詢有效,但有一些查詢,例如下面列出的查詢,需要不同的打印空白。 我希望它能夠在沒有多個打印空白的情況下工作。

我嘗試將 foreach 循環直接放在查詢下,但它找不到要打印的正確項目。

 List<Item> items = new List<Item>();//create list

 items.Add(new Item()
            {

                ItemGroup = "Electric",
                ItemDescription = "Electric Sander",
                Quantity = 7,
                UnitPrice = 59.98f,
            });//add list item

 var totalValue = items
            .Select(x => (
                x.ItemDescription,
                x.Quantity,
                x.UnitPrice,
                x.Price = TotalValue((int)x.Quantity, (float)x.UnitPrice)
                ))
            .OrderBy(x => x.Quantity)
            .ToList();//create new list for question 1

         PrintItems(totalValue);//print item (throwing error "can't 
 //convert list to item"

  static void PrintItems(List<Item> items)//print void
    {
        foreach (var Item in items)
        {
            Console.WriteLine("" + Item);
        }
    }

您正在使用 C# 7 功能使用 LINQ Select創建元組,這意味着您不再擁有List<Item>類型,而是List<yourTupleType>

如果這就是您要在 Select 中計算的全部內容,那么為什么不將TotalValue移動到Item class

public class Item
{
    public string ItemGroup { get; set; }
    public string ItemDescription { get; set; }
    public int Quantity { get; set; }
    public float UnitPrice { get; set; }

    public float Price
    {
        get
        {
            // your TotalValue logic since you already have quantity and unit Price
            // something like
            return Quantity * UnitPrice;
        }
    }
}

然后您可以訂購您的收藏並傳遞給 PrintItems 方法。

如果您想在Item class 之外使用TotalValue方法,則另一種選擇 - 不要創建新的 object,而只需重新計算每個項目的價格。

items.ForEach(i => i.Price = TotalValue((int)x.Quantity, (float)x.UnitPrice));
PrintItems(items.OrderBy(i => i.Quantity).ToList());

您的Select()語句返回的是一個元組。 而且元組不能轉換為項

在 C# 中,當您編寫(1, "hello", 1.0f)時,您正在創建一個ValueTuple<int, string, float>或簡而言之(int, string, float)類型的元組,它不能與像 class

class NotTuple
{
    public int PropInt { get; set; }
    public string PropString { get; set; }
    public float PropFloat { get; set; }
}

(因為他們有不同的類型!)

因此,當你這樣做

.Select(x => (
       x.ItemDescription,
       x.Quantity,
       x.UnitPrice,
       x.Price = TotalValue((int)x.Quantity, (float)x.UnitPrice)
    ))

您創建一個IEnumerable<(string, int, float, float)> ,而不是一個IEnumerable<Item> 稍后當您調用.ToList()時,IEnumerable 被消耗並具體化為List<(string, int, float, float)> ,它不能與List<Item>互換。

你能跳過 PrintItems 方法嗎? 如果你能... 這樣做

        List<Item> items = new List<Item>();//create list

    items.Add(new Item()
        {

            ItemGroup = "Electric",
            ItemDescription = "Electric Sander",
            Quantity = 7,
            UnitPrice = 59.98f,
        });//add list item

         var totalValue = items.GroupBy(a => new { a.Quantity, a.ItemDescription, a.UnitPrice })
                  .Select(g => new { g.Key.ItemDescription, g.Key.Quantity, g.Key.UnitPrice,
                                     Price = g.Sum(s => (int)g.Key.Quantity * (float)g.Key.UnitPrice)
                                     });

    foreach (var Item in totalValue)
    {
        Console.WriteLine("" + Item);
    }

問題出在你放在 foreach 中的變量的名稱上,它不能有一個類型的名稱。 試試別的名字。

例子:

List<Item> items = new List<Item>();//create list

 items.Add(new Item()
            {

                ItemGroup = "Electric",
                ItemDescription = "Electric Sander",
                Quantity = 7,
                UnitPrice = 59.98f,
            });//add list item

 var totalValue = items
            .Select(x => (
                x.ItemDescription,
                x.Quantity,
                x.UnitPrice,
                x.Price = TotalValue((int)x.Quantity, (float)x.UnitPrice)
                ))
            .OrderBy(x => x.Quantity)
            .ToList();//create new list for question 1

         PrintItems(totalValue);//print item (throwing error "can't 
 //convert list to item"

  static void PrintItems(List<Item> items)//print void
    {
        foreach (var ItemTest in items)
        {
            Console.WriteLine("" + ItemTest);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM