C# linq lambda 联接并选择语法



我正在寻找 Msql 查询的对应项:

SELECT per.*,add.addressDescription FROM Persons per 
       JOIN Address add ON per.AddressId = add.AddressId

我有这个查询:

    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其他属性的情况下填充Persons.addressDescription?想象一下,如果Persons还有 10 个属性。

我想避免使用这样的循环:

foreach(Person person in PersonList)
{
  foreach(Address address in AddressList)
  {
     if(person.addressId == address.addressId){
        person.addressDescription = address.addressDescription
     }
   }
}
var query = persons.join(address,
    per = person.addressId,
    add = addressId
    (per,add) => 
    {
        per.addressDescription = add.addressDescription;
        return per;
    });
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

相关内容

  • 没有找到相关文章

最新更新