{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/unity-linq-usage-performance/",
  "atlas": "https://www.pystone.net/?node=unity-linq-usage-performance#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/unity-linq-usage-performance.md",
  "context": "https://www.pystone.net/notes/unity-linq-usage-performance.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:unity-linq-usage-performance",
  "slug": "unity-linq-usage-performance",
  "title": "Linq usage & performance",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "Linq usage & performance",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "ed6435c0427cb77bd8f676bfafee9944fb3e59ce306c2339124d080fb8f7a67e",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "Linq usage & performance",
      "anchor": "linq-usage-performance",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#linq-usage-performance"
    },
    {
      "depth": 2,
      "text": "Introduction",
      "anchor": "introduction",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#introduction"
    },
    {
      "depth": 2,
      "text": "Usage",
      "anchor": "usage",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#usage"
    },
    {
      "depth": 3,
      "text": "Demo",
      "anchor": "demo",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#demo"
    },
    {
      "depth": 3,
      "text": "Advantages",
      "anchor": "advantages",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#advantages"
    },
    {
      "depth": 3,
      "text": "用法理解",
      "anchor": "用法理解",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#%E7%94%A8%E6%B3%95%E7%90%86%E8%A7%A3"
    },
    {
      "depth": 2,
      "text": "Performance",
      "anchor": "performance",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#performance"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/unity-linq-usage-performance/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:game-graphics-and-runtime",
      "title": "游戏图形与运行时",
      "url": "https://www.pystone.net/notes/game-graphics-and-runtime/",
      "atlas": "https://www.pystone.net/?node=game-graphics-and-runtime#knowledge-atlas",
      "label": "游戏图形与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "游戏性能优化中的“Linq usage & performance”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#%E6%B8%B8%E6%88%8F%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96"
    }
  ],
  "contentMarkdown": "# Linq usage & performance\n\n> 创建时间：2021/2/1 12:11\n\n## Introduction\n\nLINQ(Language Integrated Query, pronounced “link”), is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.\n\nLINQ extends the language by the addition of query expressions, which are akin to SQL statements, and can be used to conveniently extract and process data from arrays, enumerable classes, XML documents, relational databases, and third-party data sources.\n\n## Usage\n\n### Demo\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n\n### Advantages\n\n  * 提高可读性(readable )\n\n  * 增加简洁性(concise)\n\n  * 加快开发效率\n\n### 用法理解\n\nLINQ = 集合 + 操作符 + 具体操作\n\n  * 集合：C# 中的 Array、Dictionary、List、Stack、Queue 都是集合，都是可以用 LINQ 的。\n\n  * 操作符： Where、Select、GroupBy、Distinct 都是操作符。\n\n  * 操作：操作指的是foreach/ForEach/Single/First/ToList/ToDictionary/ToHashSet等等\n\n## Performance\n\nSQL-style LINQ queries are a concise, readable way of performing various tasks dealing with all kinds of collections. Surely all that convenience comes with a performance cost to it.\n\nLINQ 在执行过程中会产生一些临时变量，而且会用到委托（lambda 表达式）。使用委托作为条件判定方法，时间开销较高，并且会造成一定的堆内存分配。\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n\n## Ref\n\n<https://en.wikipedia.org/wiki/Language_Integrated_Query>\n<https://zhuanlan.zhihu.com/p/161681422>\n<https://www.jacksondunstan.com/articles/4819>\n"
}
