2016-07-11 1 views
3

を選択C#のLINQラムダは、私はMSQLクエリのカウンターパートを探しています参加し、構文

var query = persons.JOIN(address,per = person.addressId,add = addressId 
    (per,add) => 
    new Persons{ 
       addressDescription = add.addressDescription, 
       PersonId = per.PersonId, 
       PersonFirstName = per.PersonFirstName 
       PersonLastName = per.PersonLastName}) 

個別に割り当てずPersons.addressDescriptionを移入する方法はありますPersonsの他のプロパティ? Personsにさらに10の特性があると想像してください。

私のようなループの使用を控えたいと思います:

foreach(Person person in PersonList) 
{ 
    foreach(Address address in AddressList) 
    { 
    if(person.addressId == address.addressId){ 
     person.addressDescription = address.addressDescription 
    } 
    } 
} 
+0

の可能な複製を(HTTP [インナーの構文はSQLにLINQに参加されては?]:// stackoverflowの.com/questions/37324/what-is-the-syntax-for-an-join-in-linq-to-sql) –

+0

@ shA.tお返事ありがとう、問題は質問のみですPersons.AddressDescriptionを作成せずにPersonを返す方法。 – user3770093

答えて

3
var query = persons.join(address, 
    per = person.addressId, 
    add = addressId 
    (per,add) => 
    { 
     per.addressDescription = add.addressDescription; 
     return per; 
    }); 
+0

本当に、これは初めてselect文で復帰したときです。私は今すぐ行くよ。 – user3770093

+0

クエリ内でリターン関数を使用することはできません。/ – user3770093

+0

@ user3770093私のために何をしたかを表示します。 –

0
var id = 1; 
var query = database.Posts // your starting point - table in the "from" statement 
    .Join(database.Post_Metas, // the source table of the inner join 
     post => post.ID,  // Select the primary key (the first part of the "on" clause in an sql "join" statement) 
     meta => meta.Post_ID, // Select the foreign key (the second part of the "on" clause) 
     (post, meta) => new { Post = post, Meta = meta }) // selection 
    .Where(postAndMeta => postAndMeta.Post.ID == id); // where statement 
+0

あなたの返事をありがとうが、私はまだあなたの例に従ってそれをループする必要があると思う。 – user3770093

関連する問題