• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:【C#】LINQについての質問です。)

【C#】LINQのコードでラムダ式のtとsは何を指しているのか教えてください!

AKARI0418の回答

  • ベストアンサー
  • AKARI0418
  • ベストアンサー率67% (112/166)
回答No.1

>tとsが何にを指しているのかということです。 その部分は引数の数を指しているに過ぎませんよ。 この部分を理解するには、 orders.Aggregate()のとる引数に注目することが必要です。 たぶん、 第2パラメータがFunc(x,y)のような感じになっていませんか? これはデリゲートが引数になっているのです。 要するに、引数として (t, s) => t.Amount > s.OrderAmount ? t : new { Amount = s.OrderAmount, Month = s.Month } という式を渡してあげているのです。 そのため、 tとsにはその式内での仮名に過ぎませんので、 意味は特にないですね。 hogeとかでも識別できればよいことになります。

oboroxx
質問者

お礼

申し訳ございません、理解いたしました。 デリゲートのFunc<>だと、引数の型指定もされている事を忘れていました。 申し訳ございません。 解決しましたので閉じさせていただきます。

oboroxx
質問者

補足

早速の回答ありがとうございます。 おっしゃっている内容はわかるのですが、本にはAggregateについて以下のように書いてあります。 public static TAccumulate Aggregate<TSource,TAccumulate>( this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func); 上のはオーバーロードの一例ですが、私がわからないのは、上ではfuncの引数に対して何にも語られていません。 ということは、なぜ、 (t, s) => t.Amount > s.OrderAmount ? t : new { Amount = s.OrderAmount, Month = s.Month } で、tが何になり、sが何になるのかはどうやって決まるのかという事がわからないのです。 そういうものだとして、受け入れればいいのかもしれませんが、自分で書くときに、理解していないと書けないと思い質問させていただきました。 そのあたりについて何かおわかりの事がありましたらよろしくお願いいたします。

関連するQ&A

  • C#についての質問(主にLinq関係)

     ずっと独学でプログラミングをやっている者です。 C#を用いて現在開発しているのですが、たまたま見つけたLinq構文がとても綺麗だと思い、 現在練習中です。  そこで少し疑問点が出てきたので誰か回答お願いします><  例えば、「ある整数の配列の中から、偶数を探し出し、見つかった要素の数字を見つかった要素の個数分加算する」というような問題があったとします(一例ですが...)。 普通に書くと...(ちょっと上下に切り詰めてます><) int[] ary = new int[]{2, 1, 5, 4, 5, 2}; //<<<今後この行は割愛します^^ int count = 0; for(int i = 0; i < ary.Length; i++) if(ary[i] % 2 == 0) count++; for(int i = 0; i < ary.Length; i++) if(ary[i] % 2 == 0) ary[i] += count; //結果は{5, 1, 5, 7, 5, 5} みたいな感じでしょうか。 これでも全然いいのですが、条件を二回使用しているのもなんだかすっきりしないですし、 IEnumerableなコレクション全てに対応させるには少し不向きな書き方ですよね? forEachも考えましたが、forEachだと最後の行が... forEach(int i in ary.slect(new {val = ary[i], ix = i}) if(i.val % 2 == 0) ary[i.ix] += count; みたいな回りくどい方法しか今のところ思いつきません。 理想は... var where = ary.where(v => (v % 2 == 0)); int count = where.count(); Array.ConvertAll(where, v => v + count); //<<<もちろんコンパイルエラー^^ こんな感じでかけたらいいのになぁとか思ってます。 最後の行を... ary = ary.select(v => v + count).ToArray(); とすれば似たようなことはできるのですが、 これだと新しい配列が出来てしまいますよね? 今回の例では構わないのかもしれませんが、 大きな配列になってくるとメモリが心配になる書き方なので少し気持ち悪いです>< それとも現代ではそんなことはお構いなしなのかなぁ...とか思うときもありますが^^ (↑なんせ独学なもので常識知らずです><) なにかすっきりとした実装方法は無いでしょうか? それとも今回のような配列の中身を更新、変換するような場合には、 Linqを用いること自体が推奨されていないのでしょうか?  ずっと、C++と同じような方法でコレクションを扱ってきていたので、 ガベージコレクションの挙動も良くわかっていません。 deleteのし忘れを防ぐぐらいの機能なのか、配列だろうがどんどん新しくメモリを確保しても 大丈夫な機能なのか...?です。 Linqのような構文もまだまだ付け焼刃です。 もしかしたら、結構基本的な所を見落としている可能性もあります>< どなたかご教授お願いしますm(-_-)m

    • 締切済み
    • CSS
  • C# Linq To XML について

    C#をつかって、XMLファイルを読み出し、値を検索しようとしていますが、ほしい情報をとることができません。もしわかる方がいましたら、ご回答よろしくおねがいします。 XMLは下記の形式で、このtableが4つ続いてあります。 <table> <row> <column name="Tests">Tests Textvalues</column> </row> <row> <column name="A">Id</column> <column name="B">Description</column> <column name="C">Value</column> </row> <row> <column name="A">1</column> <column name="B">no</column> <column name="C">34 25 E0</column> </row> <row> <column name="A">2</column> <column name="B">yes</column> <column name="C">34 25 E1</column> </row> </table> <table>.....つづく 下記のようなコードを書きました。 XElement xelement = XElement.Load(ファイルパス); IEnumerable<XElement> tables = xelement.Elements(); foreach (XElement table in tables) { var name = from nm in table.Elements("row") where (string)nm.Element("colmun").Attribute("B")== "no"              select nm; foreach (var a in name) { Console.WriteLine(a); } } 残念ながら、このコードでは、nameの中身がからっぽです。 目的は、Bの値からCの値を検索することです。 以上、よろしくお願いします。

  • C言語、半角で打ち直しても多くのエラーメッセージ。

    いつも大変お世話になり、誠にありがとうございます。 標記の件。 下記に、コード、エラーメッセージを書きます。 コンパイルしたところ、多くのエラーメッセージが出ます。 こういうエラーメッセージの場合、誤って、全角で 打ってしまったのだと思い、全部を半角で打ち直します。 デモ、また大量のエラーメッセージが出ます。 全角スペースを半角で打ち直す「コツ」ってありますか? 是非とも教えてください。 ご多忙中のところ、度々申し訳ございません。 下記コードが何故コンパイルエラーを起こすのか どうか、ご回答の程宜しくお願い申し上げます。        記 コード /*文字と数値を出力する*/ #include <stdio.h> int main(void) { printf("%cは文字です。\n",'A'); printf("%dは整数です。\n",123); printf("%fは小数です。\n",10.5); return 0; } エラ-メッセージ C:\MinGW>gcc test1.c -o test1 test1.c:1:1: error: stray '\377' in program test1.c:1:2: error: stray '\376' in program test1.c:1:3: error: expected identifier or '(' before '/' token test1.c:1:4: warning: null character(s) ignored test1.c:1:6: warning: null character(s) ignored test1.c:1:7: error: stray '\207' in program test1.c:1:17: error: stray '\222' in program test1.c:1:19: error: stray '\372' in program test1.c:1:21: error: stray '\233' in program test1.c:1:25: error: stray '\213' in program test1.c:1:28: warning: null character(s) ignored test1.c:1:30: warning: null character(s) ignored test1.c:2:1: warning: null character(s) ignored # i n c l u d e < s t d i o . h > ^ test1.c:3:1: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:3: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:4: error: invalid preprocessing directive #i i n t m a i n ( v o i d ) ^ test1.c:3:5: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:7: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:9: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:11: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:13: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:15: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:17: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:21: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:23: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:25: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:27: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:29: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:31: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:33: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:35: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:3:37: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ test1.c:4:1: warning: null character(s) ignored { ^ test1.c:5:1: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:3: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:5: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:7: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:11: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:13: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:15: warning: null character(s) ignored p r i n t f ( " % c o0アールW[g0Y00\ n " , ' A ' ) ; ^ test1.c:5:17: warning: null character(s) ignored 全部はコピーできませんでしたが、どうぞ、宜しくお願い申し上げます。

  • java 緊急質問

    java プログラムの課題です このプログラムはファイルを読み込むと最初に十数行の空行が出来てしまいます またファイル内のメールアドレスのみを読み取りたいのですが@の入っていない文も読み取ってしまいます。 あと読み込んだメールアドレスの数だけカウントしたいのですがそこもうまくいっていません。 @を含む一文だけを読み取るようにしたいです。 import java.io.*; public class Email11 { public static void main(String[] argv) throws Exception { String input = "fileContainingEmails.txt"; String output = "copyPasteMyEmails.txt"; BufferedReader cin; cin = new BufferedReader(new InputStreamReader(System.in)); String userInput; System.out.println("Enter input file name [default name: fileContainingEmails.txt]"); userInput = cin.readLine(); if (userInput.equals("")) userInput = input; String name; if (userInput.equals(input)) name = output; else name = userInput; String userOutput; System.out.println("Enter output file name [default name: " + name + " ]"); userOutput = cin.readLine(); if (userOutput.equals("")) userOutput = name; System.out.println("Input FIle : " + userInput); System.out.println("Output FIle : " + userOutput); BufferedReader fin; fin = new BufferedReader(new FileReader(userInput)); String lineFromFile = ""; while (true) { if (!fin.ready()) break; fin.readLine(); lineFromFile = fin.readLine(); System.out.println(lineFromFile.substring(0, lineFromFile.indexOf('@')+1)); PrintWriter fout; fout = new PrintWriter(new FileWriter(userOutput, true)); fout.println(lineFromFile.substring(0, lineFromFile.indexOf('@')+1)); fout.close(); } fin.close(); int count = 0; for (int p = 0; p < lineFromFile.length(); p++) { if (lineFromFile.equals('@')); count++; } if (count == 0) System.out.println("Sorry, no email addresses were found in the file " + userInput); else System.out.println(count + "email addresses were found, and copied to " + userOutput); どのように書き直したらよいのでしょうか?

    • ベストアンサー
    • Java
  • 数学で質問です

    X, Y,Z 空間で、O(0,0,0)           A(3,0,0)           B(3,2,0)           C(0,2,0)           D(0,0,4)           E(3,0,4)           F(3,2,4)           G(0,2,4)を頂点とする           直方体OABCーDEFGを考える。                                        辺AEをs:1-sに内分する点をP、                                    辺CGをt: 1-tに内分する点をQ  とおく。                                    ただし、0<s<1、0<t<1とする。          Dを通り、O、P、Qを含む平面に垂直な直線が、線分AC(両端を含む)と交わるようなs、t の満たす条件を求めよ。 Dを通り、O、P、Qを含む平面に垂直な直線をしめす方法がわかりません。

  • C言語に関する質問

    初めて質問させて頂きます。C言語初心者です。 実は講義で「ファイル中の英文を単語に分けてその出現頻度をカウントするコードを木構造を用いて出力せよ」という課題が出ました。 そこで、参考にするコードを検索しましたところ、以下のURLにあるベストアンサーのコードが近いと感じました。 http://okwave.jp/qa/q4155655.html コードの内容は以下の通りになります。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct node Node; struct node{ char *word; int count; Node *left,*right; }; Node *root=NULL; void compose(FILE *fp); void inorder(Node *p); void strlower(char *s); int main(int argc, char *argv[]) { FILE *fp; Node *new; fp=fopen(argv[1],"r"); if(fp==NULL){puts("ファイルを開けません");return(-1);} compose(fp); inorder(root); return (0); } void strlower(char *s){ while(*s!=NULL){*s=tolower(*s);s++;} } void compose(FILE*fp){ Node **p,*new; char buf[256]; while(1){ fscanf(fp,"%[^a-zA-Z0-9]",buf); if(fscanf(fp,"%[a-zA-Z0-9]",buf)==EOF)break; strlower(buf); if(root==NULL){ new=(Node *)malloc(sizeof(Node)); new->left=NULL; new->right=NULL; new->word=strdup(buf); new->count=1; root=new; }else{ *p=root; while(1){ if(strcmp(buf,(*p)->word)==0){ (*p)->count++;break; }else if(strcmp(buf,(*p)->word)<0){ if((*p)->left==NULL){ new=(Node *)malloc(sizeof(Node)); new->left=NULL;new->right=NULL;new->word=strdup(buf);new->count=1; (*p)->left=new; break; }else{ *p=(*p)->left; } }else{ if((*p)->right==NULL){ new=(Node *)malloc(sizeof(Node)); new->left=NULL; new->right=NULL; new->word=strdup(buf); new->count=1; (*p)->right=new; break; }else{ *p=(*p)->right; } } } } } } void inorder(Node*p){ if (p==NULL) return; inorder(p->left); printf("%s %d\n",p->word, p->count); inorder(p->right); } しかし、これをそのままコンパイル・実行すると、コンパイル時に以下の注意が出ます。 warning comparison between pointer and integer ('int' and 'char *') while(*s!=NULL){*s=tolower(*s);s++;} 上記の注意を無視してそのまま実行すると、segmatation faultが出てしまいますorz おそらく、sの型が*s=s[]なので、注意の中の「s++」の部分で誤作動を起こしている(s++を実行するにはsはint型でなければならない)と思うのですが、どうコード文を変えれば良いのかがよくわかりません。 どなたかお教え頂けると幸いです。どうぞよろしくお願いしますm(_ _)m

  • ファイルデータの入れ替えについての質問

    お世話になります ファイルの内容は $s_get_year<>$s_get_month<>@chk<>\n このような並び方です POSTメソッドで送られてきたデータ$get_yearと$get_monthが 一番目のデータ($s_get_year)と2番目のデータ($s_get_month)と同じならば その行を入れ替えて、そうでなければ追加して書き込むという処理を行いたいのですが 下記のようにしたのですが思い通りの動作をしません、御教授お願いします。 sub ho_disc_write { @new=(); open(IN,"$holidayfile") || &error("Open Error : $holidayfile"); while (<IN>) { ($s_get_year,$s_get_month) = split /<>/, $_, 2; if ($s_get_year == $get_year && $get_month == $s_get_month) { $_ = join('<>',$get_year,$get_month,@chk,"\n"); } push(@new,$_); } close(IN); # データ上書き open(OUT,">$holidayfile") || &error("Write Error : $holidayfile"); print OUT @new; close(OUT); exit; }

    • ベストアンサー
    • Perl
  • PHPのPOSTでの半角スペース

    PHP5.1.6を使用しています。 POSTで以下のようにnameの値に半角スペースを含め <input type="hidden" name="p o s t" value="p o s t"> 以下のようにデータを受け取って表示させると foreach($_POST as $k=>$v){ print $k."=".$v."<br>\n"; } p_o_s_t=p o s t と表示されます。nameに半角スペースが含まれると強制的にアンダースコアに変換されているように思われます。valueの方は半角スペースのまま表示されます。 これはPHPの仕様なのでしょうか。Perlではこのようなことはないようです。 この点について詳しい情報をお持ちの方はお教えいただきたく思います。 よろしくお願いいたします。

    • ベストアンサー
    • PHP
  • C++ ポインタについて質問です

    c++ で三角形の周囲を計算で出したいのですが、エラーがでてしまいます。うまくpoint が機能していないようなのですがどこが悪いのでしょうか?? main.cppの中身 #ifdef TRIANGLE_DONE { // Define three points Point p1 = {1, 3}, p2 = {-2, -2}, p3 = {3, -1}; // Create a Triangle variable Triangle t = {p1, p2, p3}; // Now, test the perimeter() function assertDoubleEqualsMsg("Check perimeter of Triangle", 15.4021, perimeter(t), 1E-5); } #else cerr << "Define the Triangle structure. Then, uncomment #define TRIANGLE_DONE in h19.h" << endl; #endif hの中身 #define TRIANGLE_DONE struct Triangle{ Point a, b, c; }; #ifdef TRIANGLE_DONE /** * Calculates the perimeter of the Triangle t. * @param t the Triangle to examine. * @return the perimeter. */ double perimeter(const Triangle& t); #endif h19.cppのなかみ double premiter(const Triangle& t) { double p = 0; p = t.p1 + t.p2 + t.p3; return p; } 必ず "Define the Triangle structure. Then, uncomment #define TRIANGLE_DONE in h19.h" のコメントがでてしまいます。どのように改善すれば良いでしょうか?

  • C言語、最初からやり直すつもりが異変が?

    いつも大変お世話になり誠にありがとうございます。 標記の件。 C言語を最初からやり直すつもりがコンパイル時に 理解できないエラーが発生しました。 原因はなんでしょうか? 度々すいません。 ご回答の程宜しくお願い申し上げます。          記 1.コード #include <stdio.h> int main(void) { printf("ようこそC言語へ!\n"); return 0; } 2.コンパイル C:\MinGW>gcc text1.c -o text1 text1.c:1:1: error: stray '\377' in program text1.c:1:2: error: stray '\376' in program text1.c:1:3: error: stray '#' in program text1.c:1:4: warning: null character(s) ignored text1.c:1:6: warning: null character(s) ignored text1.c:1:5: error: unknown type name 'i' text1.c:1:8: warning: null character(s) ignored text1.c:1:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'c' text1.c:1:10: warning: null character(s) ignored text1.c:1:9: error: unknown type name 'c' text1.c:1:12: warning: null character(s) ignored text1.c:1:14: warning: null character(s) ignored text1.c:1:16: warning: null character(s) ignored text1.c:1:18: warning: null character(s) ignored text1.c:1:22: warning: null character(s) ignored text1.c:1:24: warning: null character(s) ignored text1.c:1:26: warning: null character(s) ignored text1.c:1:28: warning: null character(s) ignored text1.c:1:30: warning: null character(s) ignored text1.c:1:32: warning: null character(s) ignored text1.c:1:34: warning: null character(s) ignored text1.c:1:36: warning: null character(s) ignored text1.c:1:38: warning: null character(s) ignored text1.c:2:1: warning: null character(s) ignored ^ text1.c:3:1: warning: null character(s) ignored i n t m a i n ( v o i d ) ^ text1.c:4:1: warning: null character(s) ignored { ^ text1.c:5:1: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:3: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:5: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:7: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:11: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:13: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:15: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:17: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:19: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:21: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:23: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:25: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:27: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:5:29: warning: null character(s) ignored p r i n t f ( " ・F0S0]0C 萱岳0text1.c:6:1: warning: null character(s) ignored ^ text1.c:7:1: warning: null character(s) ignored r e t u r n 0 ; ^ text1.c:7:3: warning: null character(s) ignored r e t u r n 0 ; ^ text1.c:8:1: warning: null character(s) ignored } ^ text1.c:9:1: warning: null character(s) ignored text1.c:9:11: warning: null character(s) ignored text1.c:9:13: warning: null character(s) ignored text1.c:9:15: warning: null character(s) ignored text1.c:9:17: warning: null character(s) ignored text1.c:9:19: warning: null character(s) ignored text1.c:9:21: warning: null character(s) ignored text1.c:9:23: warning: null character(s) ignored text1.c:9:24: warning: null character(s) preserved in literal text1.c:9:49: warning: null character(s) ignored text1.c:9:24: warning: unknown escape sequence: '\000' text1.c:9:51: warning: null character(s) ignored text1.c:9:53: warning: null character(s) ignored text1.c:10:1: warning: null character(s) ignored text1.c:11:1: warning: null character(s) ignored text1.c:12:1: warning: null character(s) ignored text1.c:13:1: warning: null character(s) ignored 全部は入力できませんでした。 誠に申し訳ありません。 ご回答の程宜しくお願い申し上げます。