投稿数 - 437, コメント - 6221, トラックバック - 156

LINQ Tips - 複数のキーを使って join

C# には join ステートメントがあり、2つのコレクションを結合できるのだが、結合のためのキーが一つしか指定できない。どうしたら複数のキーを使って結合できるのか?

キーに匿名型を使えば良いだけ。

using System;
using System.Linq;

class Data
{
    public int X { get; set; }
    public int Y { get; set; }
    public override string ToString()
    {
        return String.Format("X = {0}, Y = {1}", X, Y);
    }
}

class Program
{
    static void Main()
    {
        var data1 = new[] {
            new Data { X = 1, Y = 1 },
            new Data { X = 2, Y = 2 },
            new Data { X = 3, Y = 3 },
            new Data { X = 4, Y = 4 },
            new Data { X = 5, Y = 5 },
        };

        var data2 = new[] {
            new Data { X = 1, Y = 5 },
            new Data { X = 2, Y = 4 },
            new Data { X = 3, Y = 3 },
            new Data { X = 4, Y = 2 },
            new Data { X = 5, Y = 1 },
        };

        Console.WriteLine("単一の値で join");

        var query1 = from d1 in data1
                     join d2 in data2 on d1.X equals d2.X
                     select d1;

        query1.ToList().ForEach(Console.WriteLine);

        Console.WriteLine("複数の値で join");

        var query2 = from d1 in data1
                     join d2 in data2 on new { d1.X, d1.Y } equals new { d2.X, d2.Y }
                     select d1;

        query2.ToList().ForEach(Console.WriteLine);
    }
}

 join_run

投稿日時 : 2008年8月31日 12:31

フィードバック

# re: LINQ Tips - 複数のキーを使って join

ほー。
どうも equals での比較には IEqualityComparer が使われるようですな。
2008/08/31 17:44 | シャノン

# re: LINQ Tips - 複数のキーを使って join

なーんも考えんと普通に Equals 使っとるんかと思ってた。
2008/09/02 0:21 | 囚人

# http://lvshoulderbagbuy.sinaapp.com/

?婁笘?便伀?倞????傘?????亜?樹??с????
2012/10/20 2:59 | Louis Vuitton

# Wonderful blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks

Wonderful blog! I found it while browsing on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I've been trying for a while but I never seem to get there!
Thanks

# Link exchange is nothing else however it is just placing the other person's blog link on your page at suitable place and other person will also do similar in favor of you.

Link exchange is nothing else however it is just
placing the other person's blog link on your page at suitable place and other person will also do similar
in favor of you.

# I constantly spent my half an hour to read this webpage's articles all the time along with a cup of coffee.

I constantly spent my half an hour to read this webpage's articles all the
time along with a cup of coffee.

# Can you tell us more about this? I'd like to find out more details.

Can you tell us more about this? I'd like to find out more details.

# Asking questions are really good thing if you are not understanding something fully, except this piece of writing offers fastidious understanding yet.

Asking questions are really good thing if you are not understanding
something fully, except this piece of writing offers fastidious understanding yet.

# Fascinating blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your theme. Cheers

Fascinating blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would
really make my blog stand out. Please let me know where you got your theme.
Cheers

# When someone writes an post he/she retains the idea of a user in his/her mind that how a user can be aware of it. Therefore that's why this piece of writing is outstdanding. Thanks!

When someone writes an post he/she retains the idea
of a user in his/her mind that how a user can be aware of it.
Therefore that's why this piece of writing is outstdanding.
Thanks!

# After looking into a few of the articles on your website, I really appreciate your way of writing a blog. I saved as a favorite it to my bookmark site list and will be checking back soon. Take a look at my web site as well and let me know what you think.

After looking into a few of the articles on your website,
I really appreciate your way of writing a blog.
I saved as a favorite it to my bookmark site list and will be checking back soon. Take a look
at my web site as well and let me know what you think.

# Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks

Sweet blog! I found it while searching on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?

I've been trying for a while but I never seem to get there!
Thanks

# I every time spent my half an hour to read this web site's posts everyday along with a mug of coffee.

I every time spent my half an hour to read this web site's posts everyday
along with a mug of coffee.

# Can I just say what a comfort to find someone who genuinely knows what they're discussing on the web. You actually know how to bring a problem to light and make it important. More and more people really need to check this out and understand this side o

Can I just say what a comfort to find someone who genuinely knows what they're discussing on the web.
You actually know how to bring a problem to light and
make it important. More and more people really need to check this out
and understand this side of the story. I
was surprised that you are not more popular because you
certainly possess the gift.

# Hey! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to prevent hackers?

Hey! I just wanted to ask if you ever have any
trouble with hackers? My last blog (wordpress) was hacked and
I ended up losing many months of hard work due to no
backup. Do you have any solutions to prevent hackers?

# Good day! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!

Good day! I know this is kinda off topic but I was wondering if you
knew where I could locate a captcha plugin for my comment form?

I'm using the same blog platform as yours and I'm having difficulty
finding one? Thanks a lot!

# Fastidious response in return of this difficulty with genuine arguments and telling everything about that.

Fastidious response in return of this difficulty with genuine arguments and telling everything about that.

# My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on several websites for about a year and am nervous about switching to anothe

My programmer is trying to persuade me to move to .net from
PHP. I have always disliked the idea because of the costs.
But he's tryiong none the less. I've been using WordPress
on several websites for about a year and am nervous about switching to another platform.
I have heard excellent things about blogengine.net. Is there a way I can import all my wordpress posts into it?
Any help would be greatly appreciated!

# certainly like your web site but you need to test the spelling on several of your posts. Several of them are rife with spelling problems and I find it very troublesome to tell the reality nevertheless I will definitely come back again.

certainly like your web site but you need to test the spelling on several of your posts.
Several of them are rife with spelling problems and I find it very
troublesome to tell the reality nevertheless I will definitely come back again.

# Can I simply just say what a relief to find an individual who truly understands what they are discussing on the internet. You actually understand how to bring a problem to light and make it important. More and more people must read this and understand

Can I simply just say what a relief to find an individual who truly understands what they are discussing on the internet.
You actually understand how to bring a problem to light and make it important.

More and more people must read this and understand this side of your story.
I can't believe you're not more popular since you certainly have the gift.

# Thankfulness to my father who stated to me about this blog, this website is really awesome.

Thankfulness to my father who stated to me about this blog, this website is really awesome.

# I've been surfing on-line greater than 3 hours as of late, yet I by no means found any attention-grabbing article like yours. It's lovely value sufficient for me. In my view, if all web owners and bloggers made excellent content material as you did, the

I've been surfing on-line greater than 3 hours as of late,
yet I by no means found any attention-grabbing article like yours.
It's lovely value sufficient for me. In my view, if all web owners
and bloggers made excellent content material as you did, the web shall be
much more helpful than ever before.

# Fantastic beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog website? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear idea

Fantastic beat ! I would like to apprentice while you amend your
web site, how could i subscribe for a blog website?

The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear
idea

# I am sure this piece of writing has touched all the internet viewers, its really really pleasant paragraph on building up new weblog.

I am sure this piece of writing has touched all the internet viewers,
its really really pleasant paragraph on building up new weblog.

# Hi, i think that i saw you visited my site so i came to “return the favor”.I am trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!

Hi, i think that i saw you visited my site so i came to “return the favor”.I am trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!

# wonderful points altogether, you simply gained a brand new reader. What would you suggest in regards to your publish that you just made some days ago? Any sure?

wonderful points altogether, you simply gained a brand
new reader. What would you suggest in regards to your publish that you
just made some days ago? Any sure?

# No matter if some one searches for his necessary thing, so he/she desires to be available that in detail, so that thing is maintained over here.

No matter if some one searches for his necessary
thing, so he/she desires to be available that in detail,
so that thing is maintained over here.

コメントの投稿

タイトル  
名前  
URL
コメント