首页 > 代码库 > LINQ使用
LINQ使用
基于扩展方法和lamda表达式
1. 查询序列中满足一定条件 Where扩展方法
public interface ISlotPortBinding { byte SlotNumber { get; set; } string PortName { get; set; } }
private List<ISlotPortBinding> _slotPortBindings;
var firstBinding = _slotPortBindings.Where(x => x.SlotNumber <= 4).ToList();
2.序列属性赋值ForEach扩展方法
firstBinding.ForEach(x => x.PortName = “name”);
部分代码参考:
public void RefreshPortNames(string slot14Port, string slot58Port) { var firstBinding = _slotPortBindings.Where(x => x.SlotNumber <= 4).ToList(); var secondBinding = _slotPortBindings.Where(x => x.SlotNumber >= 4).ToList(); var slotPortBinding = firstBinding.FirstOrDefault(); bool isFirstBindingChanged = slotPortBinding != null && slotPortBinding.PortName != slot14Port; var firstOrDefault = secondBinding.FirstOrDefault(); bool isSecondBindingChanged = firstOrDefault != null && firstOrDefault.PortName != slot58Port; if (isFirstBindingChanged) { firstBinding.ForEach(x => x.PortName = slot14Port); } if (isSecondBindingChanged) { secondBinding.ForEach(x => x.PortName = slot58Port); } if (isFirstBindingChanged || isSecondBindingChanged) { SlotNumberChangedEvent?.Invoke(_currentSlotPortBinding); } //刷新页面 }
LINQ使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。