首页 > 编程语言 >join clause (C# Reference)

join clause (C# Reference)

时间:2022-08-30 11:25:19浏览次数:81  
标签:category elements join sequence C# clause prod

join clause (C# Reference)

The join clause is useful for associating elements from different source sequences that have no direct relationship in the object model. The only requirement is that the elements in each source share some value that can be compared for equality. For example, a food distributor might have a list of suppliers of a certain product, and a list of buyers. A join clause can be used, for example, to create a list of the suppliers and buyers of that product who are all in the same specified region.

A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence. The join clause compares the specified keys for equality by using the special equals keyword. All joins performed by the join clause are equijoins. The shape of the output of a join clause depends on the specific type of join you are performing. The following are three most common join types:

  • Inner join

  • Group join

  • Left outer join

Inner join

The following example shows a simple inner equijoin. This query produces a flat sequence of "product name / category" pairs. The same category string will appear in multiple elements. If an element from categories has no matching products, that category will not appear in the results.

C#
var innerJoinQuery =
    from category in categories
    join prod in products on category.ID equals prod.CategoryID
    select new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence

For more information, see Perform inner joins.

Group join

A join clause with an into expression is called a group join.

C#
var innerGroupJoinQuery =
    from category in categories
    join prod in products on category.ID equals prod.CategoryID into prodGroup
    select new { CategoryName = category.Name, Products = prodGroup };

A group join produces a hierarchical result sequence, which associates elements in the left source sequence with one or more matching elements in the right side source sequence. A group join has no equivalent in relational terms; it is essentially a sequence of object arrays.

If no elements from the right source sequence are found to match an element in the left source, the join clause will produce an empty array for that item. Therefore, the group join is still basically an inner-equijoin except that the result sequence is organized into groups.

If you just select the results of a group join, you can access the items, but you cannot identify the key that they match on. Therefore, it is generally more useful to select the results of the group join into a new type that also has the key name, as shown in the previous example.

You can also, of course, use the result of a group join as the generator of another subquery:

C#
var innerGroupJoinQuery2 =
    from category in categories
    join prod in products on category.ID equals prod.CategoryID into prodGroup
    from prod2 in prodGroup
    where prod2.UnitPrice > 2.50M
    select prod2;

For more information, see Perform grouped joins.

Left outer join

In a left outer join, all the elements in the left source sequence are returned, even if no matching elements are in the right sequence. To perform a left outer join in LINQ, use the DefaultIfEmpty method in combination with a group join to specify a default right-side element to produce if a left-side element has no matches. You can use null as the default value for any reference type, or you can specify a user-defined default type. In the following example, a user-defined default type is shown:

C#
var leftOuterJoinQuery =
    from category in categories
    join prod in products on category.ID equals prod.CategoryID into prodGroup
    from item in prodGroup.DefaultIfEmpty(new Product { Name = String.Empty, CategoryID = 0 })
    select new { CatName = category.Name, ProdName = item.Name };

For more information, see Perform left outer joins.

 

标签:category,elements,join,sequence,C#,clause,prod
From: https://www.cnblogs.com/chucklu/p/16638629.html

相关文章

  • 阅读《计算机图形学编程(使用OpenGL和C++)》11 - 加载外部obj模型
    复杂的3D模型可以借助建模工具生成,这种工具能够在3D空间中构建任意形状并自动生成顶点、纹理坐标、顶点法向量等。模型生成后可导出成obj文件格式,这种格式有很多,OBJ文件很......
  • C#集合:字典
    字典是一种集合,其包含的元素均为键值对。字典通常用于查找或用作排序列表。框架通过IDictionary和IDictionary<TKey,TValue>接口以及一系列通用的字典类定义了标准字典协......
  • Oracle 服务名/实例名,Service_name 和Sid的区别
    Oracle服务名/实例名,Service_name和Sid的区别 Service_name和Sid的区别Service_name:该参数是由oracle8i引进的。在8i以前,使用SID来表示标识数据库的一个实例,但是在......
  • vue3+vuex 的 actions 的 使用
    <template><divclass="app">姓名:{{$store.state.nameVuex}}<button@click="btn">基本方法:修改名字</button><br/><button@click="btn1">传递值......
  • pycharm2022.2.1版本设置中文语言
    进入"File......
  • Mysql Count的区别
    1.count(1)和count(*)执行计划从执行计划来看count(1)和count()的效果是一样的。当表的数据量大些时(1W以上),对表作分析之后,使用count(1)比使用count()用时多。当......
  • mybatis批量插入时报错:syntax error, expect ‘)‘
    问题:mybatis批量插入时报错:syntaxerror,expect‘)’ 解决:是因为传入的参数list为null,在代码中加上list.size()>0的判断。mapper文件:<insertid="batchInsertSys......
  • 最短路径算法-迪杰斯特拉(Dijkstra)算法在c#中的实现和生产应用
    迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。它的主要特点是以起始点为中心向外层层扩展(广度优先遍历思想),直到扩展到终点为止......
  • 创建React项目全过程
    首先创建react项目可以先下载脚手架create-react-app(类似于vue的脚手架vue-cli)。1、打开****cmd,执行:npminstall-gcreate-react-app;全局安装。如果执行失败,可能是n......
  • css——复选框汉字不对齐
    1.复选框汉字不对齐<inputtype="checkbox"v-model="isAll"/><span>全选</span>2.input、span加上vertical-align:middle;属性input{vertical-alig......