(精华)2020年9月6日 C#基础知识点 Linq的使用场景

举报
愚公搬代码 发表于 2021/10/18 23:19:49 2021/10/18
【摘要】 一:比较返回当前项目 namespace LinqSample { public class Quote { public Stock Stock { get; set; } ...

一:比较返回当前项目

namespace LinqSample
{
    public class Quote {
        public Stock Stock { get; set; }
        public decimal Price { get; set; }
        public DateTime Date { get; set; }
    }

    public class Stock {
        public string Name { get; set; }
        public List<Quote> Quotes { get; set; }
        public override string ToString() {
            return $"{Name}: MIN {Quotes.Min(q => q.Price)} - MAX {Quotes.Max(q => q.Price)}";
        }
    }


    public partial class Program
    {
        private static void Sample01()
        {
            var stock = new Stock {Name = "Stock Demo"};
            stock.Quotes = new List<Quote>
            {
                new Quote{Stock = stock, Price = 200, Date = DateTime.Parse("2020/3/29")},
                new Quote{Stock = stock, Price = 150, Date = DateTime.Parse("2020/3/21")},
                new Quote{Stock = stock, Price = 220, Date = DateTime.Parse("2020/3/23")},
                new Quote{Stock = stock, Price = 180, Date = DateTime.Parse("2020/3/25")},
                new Quote{Stock = stock, Price = 100, Date = DateTime.Parse("2020/3/26")},
            };
            Console.WriteLine( $"Stock: {stock}");

            var minQuote = stock.Quotes.MinItem(q => q.Date);


            Console.WriteLine(minQuote.Price);
        }

    }

    public static class Sample01Extensions
    {
        public static Quote MinPrice(this IEnumerable<Quote> source)
        {
            //Aggregate累加器函数
            return source.Aggregate((t, s) => t.Price < s.Price ? t : s);
        }

        public static TSouce MinItem<TSouce, TCompareValue>(this IEnumerable<TSouce> source,
            Func<TSouce, TCompareValue> comparerExpression)
        {
            var comparer = Comparer<TCompareValue>.Default;
            return source.Aggregate((minValue, item) =>
            {
                var result = comparer.Compare(comparerExpression(minValue), comparerExpression(item));
                return result < 0 ? minValue : item;
            });
        }
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

二:改写扩展where,自定义默认条件返回

namespace LinqSample
{
    public interface IVisible
    {
        bool Visible { get; set; }
    }

    public class Customer : IVisible
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public bool Visible { get; set; }

        public Customer()
        {
            Visible = true;
        }

        public override string ToString()
        {
            return $"{Name} 今年 {Age} 岁了";
        }
    }

    public partial class Program
    {
        private static void Sample02()
        {
            var customers = new List<Customer>
            {
                new Customer{Name = "张三", Age = 20, Visible = false},
                new Customer{Name = "李四", Age = 18},
                new Customer{Name = "王五", Age = 22},
            };

            var query = from c in customers 
                                         where c.Age < 22 select c;

            foreach (var customer in query)
            {
                Console.WriteLine(customer);
            }
        }
    }

    public static class Sample02Extensions
    {
        public static IEnumerable<TSource> Where<TSource>(
            this IEnumerable<TSource> source,
            Func<TSource, bool> predicate) where TSource : IVisible
        {
            return Enumerable.Where(source, item => item.Visible == true && predicate(item));
        }
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。

原文链接:codeboy.blog.csdn.net/article/details/108371570

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。