Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中从列表对象到类的转换#_C#_Linq - Fatal编程技术网

C# c语言中从列表对象到类的转换#

C# c语言中从列表对象到类的转换#,c#,linq,C#,Linq,我是linq和c#的新手。在这里我面临着一个问题,我必须示范课堂 public class Product { public int ItemID { get; private set; } public string Title { get; set; } public string Description { get; set; } public DateTime AuctionEndDate { get; set; } public int Pric

我是linq和c#的新手。在这里我面临着一个问题,我必须示范课堂

public class Product
{
    public int ItemID { get; private set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime AuctionEndDate { get; set; }
    public int Price { get; set; }
}

public class ShoppingCart
{
    public List<Product> Products { get; set; }
}

因此,该方法不允许我返回
sortedCart.ToList()
,因为它包含列表。我怎样才能退回购物车?如果有人知道,请帮助我。谢谢

创建一个新的
ShoppingCart
实例,并将其
产品
设置为您刚才生成的排序列表:

return new ShoppingCart { Products = sortedCart.ToList() };
当然,这样原始购物车就不会被分类;这是新的(复制的)购物车。如果要对原始产品进行就地排序,应在原始产品集合上使用,而不是LINQ:

cart.Products.Sort((x,y) => x.Title.CompareTo(y.Title));

创建一个新的
ShoppingCart
实例,并将其
产品
设置为刚生成的排序列表:

return new ShoppingCart { Products = sortedCart.ToList() };
当然,这样原始购物车就不会被分类;这是新的(复制的)购物车。如果要对原始产品进行就地排序,应在原始产品集合上使用,而不是LINQ:

cart.Products.Sort((x,y) => x.Title.CompareTo(y.Title));

您需要一种从产品列表创建购物车的方法。您可以使用现有的setter或构造函数:

public class ShoppingCart
{
    public ShoppingCart(List<Product> products)
    {
        this.Products = products;
    }
...
}
公共类购物车
{
公共购物车(列出产品)
{
这个。产品=产品;
}
...
}

然后返回新的购物车(sortedCart.ToList())

您需要一种从产品列表创建购物车的方法。您可以使用现有的setter或构造函数:

public class ShoppingCart
{
    public ShoppingCart(List<Product> products)
    {
        this.Products = products;
    }
...
}
公共类购物车
{
公共购物车(列出产品)
{
这个。产品=产品;
}
...
}

然后返回新的购物车(sortedCart.ToList())

为什么需要扩展方法

假设这个列表已经被填满了

shoppingCart.Products.OrderBy(x => x.Title);
或者使用扩展方法

public static void Sort(this ShoppingCart cart)
{

    cart.Products = (from item in cart.Products
                     orderby item.Title ascending
                     select item).ToList();
}


为什么需要扩展方法

假设这个列表已经被填满了

shoppingCart.Products.OrderBy(x => x.Title);
或者使用扩展方法

public static void Sort(this ShoppingCart cart)
{

    cart.Products = (from item in cart.Products
                     orderby item.Title ascending
                     select item).ToList();
}


扩展方法将需要在适当的位置修改
购物车
,而不是创建其项目列表的新列表副本。可能返回new ShoppingCart(){Products=sortedCart.ToList();}只需创建一个构造函数,该构造函数将
列表
作为参数,并将该参数设置为Products。然后:
returnnewshoppingcart(sortedCart.ToList())
您需要设置ShoppingCart的值。Products不会返回随机列表扩展方法需要在适当的位置变异
cart
,而不是创建其项目列表的新列表副本。可能会返回new ShoppingCart(){Products=sortedCart.ToList();}只需创建一个构造函数,它将
列表
作为参数,并将该参数设置为Products。然后:
returnnewshoppingcart(sortedCart.ToList())您需要设置ShoppingCart的值。产品不会返回随机列表