zoukankan      html  css  js  c++  java
  • linq 基本用法

    新建一个类

     public class FolderBE
        {
           public string FolderOne
           { get; set; }
           public string FolderTwo
           { get; set; }
           public string FolderThree
           { get; set; }
           public string FolderFour
           { get; set; }
           public string FolderFive
           { get; set; }
        }

    然后

        List<FolderBE> listFolder = new List<FolderBE>();

                        FolderBE folder = new FolderBE();
                        folder.FolderOne = "one";
                        folder.FolderTwo = "two";
                        folder.FolderThree = "three";

                        listFolder.Add(folder);

    写一个搜索方法:

       private bool CheckFolderExist(FolderBE folder, int item)
            {
                List<FolderBE> HavelistFolder = new List<FolderBE>();

                if (item == 1)
                {
                    HavelistFolder = (from entity in listFolder
                                      where entity.FolderOne == folder.FolderOne &&
                                      string.IsNullOrEmpty(entity.FolderTwo)
                                      select entity).ToList();
                }
                else if (item == 2)
                {
                    HavelistFolder = (from entity in listFolder
                                      where entity.FolderOne == folder.FolderOne &&
                                      entity.FolderTwo == folder.FolderTwo && string.IsNullOrEmpty(entity.FolderThree)
                                      select entity).ToList();
                }
                else if (item == 3)
                {
                    HavelistFolder = (from entity in listFolder
                                      where entity.FolderOne == folder.FolderOne &&
                                      entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree
                                      select entity).ToList();
                }
                return HavelistFolder.Count == 0;
            }

    如果只需要FolderBE其中几个字段,如下:

    新建另一个类:

        public class NewFolderBE
        {
            public string FolderOne
            { get; set; }
            public string FolderTwo
            { get; set; }
            public string FolderThree
            { get; set; }

        }

     var needfolder = from entity in listFolder
                                  where entity.FolderOne == folder.FolderOne &&
                                      entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree
                               select new NewFolderBE { FolderOne = entity.FolderOne, FolderTwo = entity.FolderTwo, FolderThree = entity.FolderThree };

           List<NewFolderBE> newlist =  needfolder.ToList();

    就OK了

  • 相关阅读:
    Appium学习笔记(一)--安装与配置
    google软件测试之道--读后笔记
    Web测试中常见分享问题
    730工作总结
    Java源码分析系列
    LeetCode 21 -- Merge Two Sorted Lists
    LeetCode 20 -- Valid Parentheses
    LeetCode 15 -- 3Sum
    LeetCode 11 -- Container With Most Water
    ASP.NET Core MVC中Hangfire及EF Core的简单使用
  • 原文地址:https://www.cnblogs.com/springyangwc/p/1954578.html
Copyright © 2011-2022 走看看