可绑定 Linq 有什么好处?

发布于 2024-11-01 15:31:28 字数 229 浏览 1 评论 0原文

我是 Linq 新手,刚刚开始在项目中使用普通绑定和 sql 查询... 我遇到了 Bindable Linq 术语..

有人可以告诉我如何使用它以及它有什么用吗? 在哪里使用它?

我在 stackoverflow 和许多其他网站上遇到过它的示例,但不知道如何使用它?

如果是的话,我是否必须添加引用才能使用可绑定的 linq,而不是它们?

由于我是新手,请大家帮我解决这个问题。

I am new to Linq just started using in the projects for normal bindings and sql queries...
Than i came across Bindable Linq term..

Can someone tell me how to use it and how it is useful??
Where to use it??

I came across examples of it in stackoverflow and many other sites but couldn't find out how to use it??

Do i have to add references to use bindable linq if yes than which are they??

As i am a newbie, Pleas guys help me out with this..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ペ泪落弦音 2024-11-08 15:31:28

与 WPF/Silverlight(或任何依赖于 ObservableCollection 来通知 UI 集合更改的任何东西)结合使用时,Bindable LINQ 非常有用。

Bindable LINQ 还尝试检测查询中的 UI 依赖性并绑定到适当的事件,以便当 UI 更改时,您的查询将被重新评估,而开发人员无需编写任何代码。

Bindable LINQ 的 CodePlex 页面有一个很好的解释:

Codeplex - Bindable LINQ

Bindable LINQ is beneficial when used in conjunction with WPF/Silverlight (or anything that depends on an ObservableCollection<T> to notify the UI of changes to the collection).

Bindable LINQ also attempts to detect UI dependencies in your query and bind to the appropriate events so that when the UI changes, your query is re-evaluated without the developer having to write any code.

The CodePlex page for Bindable LINQ has a very good explanation:

Codeplex - Bindable LINQ

安静 2024-11-08 15:31:28

看看 http://clinq.codeplex.com/ ,它似乎是一个比 Bindable 更活跃的项目LINQ。

Take a look at http://clinq.codeplex.com/ which appears to be a more active project than Bindable LINQ.

怀念你的温柔 2024-11-08 15:31:28

在应用中,经常会出现需要根据源数据计算某些值的情况。让我给你一个简单的例子,在现实生活中一切都可以更复杂(可绑定的Linq更适用于更复杂的情况)。例如,您需要根据订单中每个产品的原价和折扣来计算最终价格。如果订单商品(添加、删除)或折扣发生变化,我们还必须更新最终价格。如果不使用可绑定的 Linq,有 2 个选项可以实现此目的:

1) 每次订单商品(添加、删除)或折扣发生变化时,调用重新计算总价的方法。

2)调用定时器重新计算总价的方法(每5秒一次)
这些方法中的每一种都有缺点。

第一种方法有以下缺点:由于我们必须在代码中的很多地方调用重新计算总价的方法,因此我们可能会忘记在某个地方调用重新计算总价的方法。
第二种方法有以下缺点:

1)UI刷新总价有延迟

2)在订单构成和折扣没有变化的情况下,会徒劳地调用重新计算最终价格的方法,这会对性能产生不利影响

这两种方法都有以下缺点:每次重新计算最终价格的方法都会重新进行整个计算(枚举所有订单商品),而不是单独考虑某一商品的变化。这会对性能产生不利影响。
使用可绑定的 Linq,我们只需要

1) 确定如何计算最终价格

2) 使用 INotifyPropertyChangedINotifyCollectionChanged 接口。

我想向您介绍我的实现此方法的库:ObservableComputations。有了这个库,我们可以这样编写代码:

	public class Order : INotifyPropertyChanged
	{
		public ObservableCollection<OrderItem> Items {get; set;}
      
		private int _discount;
		public int Discount 
		{
			get => _discount;
			set
			{
				_discount = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Discount)));
			}
		}
      

      
		private Computing<decimal> _totalPrice;
		public Computing<decimal> TotalPrice =>  _totalPrice = _totalPrice ??
		    Items.Selecting(i => i.Price * i.Quantity).Summarizing()
			    .Using(totalOriginalPrice => 
				     totalOriginalPrice.Value 
                                         - totalOriginalPrice.Value * Discount / 100);

		public event PropertyChangedEventHandler PropertyChanged;
	}

	public class OrderItem  : INotifyPropertyChanged
	{
		private decimal _price;
		public decimal Price 
		{
			get => _price;
			set
			{
				_price = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Price)));
			}
		}  
      
		private int _quantity;
		public int Quantity 
		{
			get => _quantity;
			set
			{
				_price = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Quantity)));
			}
		}   

		public event PropertyChangedEventHandler PropertyChanged;
	}

我们需要的实际值存储在 TotalPrice.Value 中。 TotalPrice 返回 INotifyPropertyChanged 的​​实例,并在 Value 属性更改时通知您,因此您可以将此属性绑定到 WPF、Xamarin 的控件(我相信这对于 Blazor 来说也是如此)。当 Items 集合或 Discount、Quantity、Price 属性更改时,Value 属性也会更改。

在这里您可以看到其他类似库的讨论。

In applications, a situation often arises when it is necessary to compute some value on the basis of the source data. Let me give you a simple example, in real life everything can be more complicated (bindable Linq is more applicable to more complicated cases). For example, you need to compute the final price based on the original price and discounts for each product in the order. In case the order items (add, remove) or the discount changes, we must also update the final price. Without using bindable Linq, there are 2 options to achieve this:

1) Call the method of recalculating the total price each time the order items (add, remove) or discount changes.

2) Call the method of recalculating the total price by timer (every 5 seconds)
Each of these methods has disadvantages.

The first method has the following drawback: since we must call the method of recalculating the total price in many places in the code, we may forget to call the method of recalculating the total price somewhere.
The second method has the following disadvantages:

1) delay in refreshing the total price in UI

2) the method of recalculating the final price will be called in vain when the composition of the order and the discount have not changed, which can adversely affect performance

Both methods have the following drawback: the method of recalculating the final price each time brings the entire calculation anew (enumerates all order items), instead of taking into account a separate change in one item. This can adversely affect performance.
With bindable Linq, we only need

1) determine how the final price is calculated

2) use INotifyPropertyChanged and INotifyCollectionChanged interfaces.

I want to introduce you to my library implementing this approach: ObservableComputations. With this library, we can code like this:

	public class Order : INotifyPropertyChanged
	{
		public ObservableCollection<OrderItem> Items {get; set;}
      
		private int _discount;
		public int Discount 
		{
			get => _discount;
			set
			{
				_discount = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Discount)));
			}
		}
      

      
		private Computing<decimal> _totalPrice;
		public Computing<decimal> TotalPrice =>  _totalPrice = _totalPrice ??
		    Items.Selecting(i => i.Price * i.Quantity).Summarizing()
			    .Using(totalOriginalPrice => 
				     totalOriginalPrice.Value 
                                         - totalOriginalPrice.Value * Discount / 100);

		public event PropertyChangedEventHandler PropertyChanged;
	}

	public class OrderItem  : INotifyPropertyChanged
	{
		private decimal _price;
		public decimal Price 
		{
			get => _price;
			set
			{
				_price = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Price)));
			}
		}  
      
		private int _quantity;
		public int Quantity 
		{
			get => _quantity;
			set
			{
				_price = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Quantity)));
			}
		}   

		public event PropertyChangedEventHandler PropertyChanged;
	}

Actual value we need is stored in TotalPrice.Value. TotalPrice returns an instance of INotifyPropertyChanged and notifies you when Value property changes, so you can bind this property to the control of the WPF, Xamarin (I believe this will be true for Blazor too). Value property changes when Items collection or Discount, Quantity, Price properties changes.

Here you can see a discussion of other similar libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文