小编Jay*_*Jay的帖子

如何计算List <T>中每个产品的最终价格?

目标

计算List<T>C#中每个产品的最终价格.

问题

我需要计算price * quantity一个简单的产品并将结果返回给视图.

看这个语法:

var productPrice = productsPrices.Find(x => x.productId == 1); 
productPrice.finalProductPrice = 
    (productPrice.promotionalProductPrice != 0 ? 
        productPrice.promotionalProductPrice : 
        productPrice.originalProductPrice) 
    * sessionProducts.Find(x => x.id == 1).quantity;
Run Code Online (Sandbox Code Playgroud)

在上面传递的代码片段之前,有一个存储产品ID的字符串,请参阅:

string ids = string.Join(", ", sessionProducts.Select(x => x.id));
Run Code Online (Sandbox Code Playgroud)

而且我认为我需要它来设置列表finalProductPrice中每个产品的值productsPrices,但我不知道如何执行此操作.

我对这个问题的看法:

我以为我可以使用这样的东西:

productsPrices.Find(x => x.productId == productsPrices.Contains(ids))
    .finalProductPrice = 
    (productsPrices.Find(x => productsPrices.Contains(ids))
        .promotionalProductPrice != 0 ?
    productsPrices.Find(x => productsPrices.Contains(ids))
        .promotionalProductPrice : 
    productsPrices.Find(x => productsPrices.Contains(ids))
        .originalProductPrice) *
    sessionProducts.Find(x => x.id == 1).quantity;
Run Code Online (Sandbox Code Playgroud)

但是,当然,没有成功 …

c# asp.net-mvc-4

5
推荐指数
1
解决办法
4267
查看次数

如何在C#系统托盘应用程序中重复运行代码(如在计时器上一样)?

我开始使用C#中的默认Windows窗体应用程序,我改变的只是Progam.cs.从主要功能,我改变了

Application.Run(new Form1());
Run Code Online (Sandbox Code Playgroud)

Application.Run(new MyCustomApplicationContext());
Run Code Online (Sandbox Code Playgroud)

它指的是一个自定义类(MyCustomApplicationContext:ApplicationContext),它将我的程序作为系统托盘图标而不是Windows窗体运行.构造函数包含以下代码:

private NotifyIcon trayIcon = new NotifyIcon();
trayIcon.ContextMenu = new ContextMenu(
    new MenuItem[] 
    {
        new MenuItem("Exit", Exit)
    });
Run Code Online (Sandbox Code Playgroud)

这允许用户右键单击该图标,为他们提供一个带有"退出"选项的上下文菜单,该选项将运行关闭程序的功能.

在MyCustomApplicationContext的构造函数的末尾,我在名为Update()的类中调用一个递归函数,该函数执行ping函数并根据ping延迟更改系统托盘图标.

不幸的是我相信因为它是递归的,所以不允许运行任何其他代码,因此右键单击上下文菜单不会显示.我宁愿通过一个事件调用Update()函数,比如System.Timers.Timer Elapsed事件.我只是不知道事件是如何工作的或代码放在何处.

c# winforms

2
推荐指数
1
解决办法
1607
查看次数

标签 统计

c# ×2

asp.net-mvc-4 ×1

winforms ×1