Join a composite query with a from clause with a join clause

Source: Internet
Author: User

1. Use the from clause for review and query
Using System;
Using System. Linq;

Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Int [] intAry1 = {2, 3, 4, 5, 6, 77 };
Int [] intAry2 = {2, 4, 4,345, 45, 34, 23,324,243,423,213 343 };
Var query1 = from var1 in intAry1
From var2 in intAry2
Where var1 % var2 = 0
Group var2 by var2;
Foreach (var grp in query1)
{
Console. WriteLine ("{0}", grp. Key );
Foreach (var item in grp)
{
Console. WriteLine ("{0}", item );
}
Console. WriteLine ();
}
}
}
}

2. Use the join clause for internal join

The format of the join clause in an internal join is as follows:

Join element in dataSource on exp1 equals exp2

DataSource indicates the data source, which is the second dataset to be joined. Element indicates the local variable that stores the element in dataSource. Exp1 and exp2 indicate two expressions. They have the same data type and can be compared using equals. If exp1 and exp2 are equal, the current element is added to the query result.

Using System;
Using System. Linq;

Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
Int [] intAry1 = {5, 15, 25, 30, 40, 50, 60, 70 ,};
Int [] intAry2 = {10, 20, 30, 40, 50, 60 };
Var query1 = from val1 in intAry1
Join val2 in intAry2 on val1 equals val2
Select new {Val1 = val1, Val2 = val2 };
Foreach (var item in query1)
{
Console. WriteLine (item );
}
}
}
}

3. Use join clause for Group join

Sometimes you need to group the query results according to the elements in the first dataset. This requires another use of the join clause-grouping join.

Join element in dataSource on exp1 equals exp2 into kgname

The "into" keyword indicates that the data is grouped and saved to the "/" parameter.

Using System;
Using System. Linq;

Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
Int [] intAry1 = {5, 15, 25, 30, 40, 50, 60, 70 ,};
Int [] intAry2 = {10, 20, 30, 40, 50, 60 };
Var query1 = from val1 in intAry1
Join val2 in intAry2 on val1 equals val2 into valGrp
Select new {Val1 = val1, Val2 = valGrp };
Foreach (var obj in query1)
{
Console. WriteLine ("{0}", obj. Val1 );
Foreach (var item in obj. Val2)
{
Console. WriteLine ("{0}", item );
}
Console. WriteLine ();
}
}
}
}

4. Use the join clause for left Outer join

The third join is a left external join. It returns all elements in the first set, whether or not it has related elements in the second set.

In LINQ, DefaultIfEmpty () is called for the Group join result to execute the left Outer Join. The DefaultIfEmpty () method gets the specified element from the list. If the list is empty, the default value is returned.

Using System;
Using System. Linq;

Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
Int [] intAry1 = {5, 15, 25, 30, 40, 50, 60, 70 ,};
Int [] intAry2 = {10, 20, 30, 40, 50, 60, 40, 6, 2345, 23 };
Var query1 = from val1 in intAry1
Join val2 in intAry2 on val1 equals val2 into valGrp
From grp in valGrp. DefaultIfEmpty ()
Select new {Val1 = val1, Val2 = grp };
Foreach (var item in query1)
{
Console. WriteLine ("{0}", item );
}
}
}
}

Note:

The left Outer Join and Group join are similar but not the same. The query result returned by grouping join is a hierarchical data structure that requires two layers of foreach () to traverse its results. The left Outer join performs another query on the Query Result of the Group join, so it has a from clause after the join operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.