码迷,mamicode.com
首页 > 其他好文 > 详细

LINQ使用

时间:2016-09-27 20:24:12      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

基于扩展方法和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使用

标签:

原文地址:http://www.cnblogs.com/pangkang/p/5914053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!