简体   繁体   English

使用linq从链接到另一个表的表中获取一条记录

[英]using linq to get a single record from a table linked to another table

The question is confusing since I really didn't know how to ask this. 这个问题令人困惑,因为我真的不知道该怎么问。

Anyhow, I have 4 tables and I need to get to the TokenSetting table to get a single record. 无论如何,我有4个表,我需要进入TokenSetting表以获取单个记录。 I am given the companycode and I have to travel from there and get the rest of the information that I need. 我得到了公司代码,我必须从那里旅行,并获得我需要的其余信息。 And because there are multiple corporationstructures to each corporation I have to get the corporationstructure first to get the tokens assigned to it. 而且由于每个公司都有多个公司结构,因此我必须首先获得公司结构才能获得分配给它的令牌。

Here is the layout: 这是布局:

在此处输入图片说明

This is my code: 这是我的代码:

  _currentcorp = (from c in _entities.Corporations
              where c.CompanyCode == textBox_CompanyCode.Text
              select c).FirstOrDefault();

  if (_currentcorp == null)
  {
    errorProvider1.SetError(textBox_CompanyCode, "Invalid corporation.");
    return;
  }

  var corpstructure = (from cs in _currentcorp.CorporationStructures
                          where cs.District == null &&
                                cs.Branch == null &&
                                cs.Region == null
                          select cs).FirstOrDefault();

  if (corpstructure == null)
  {
    errorProvider1.SetError(textBox_CompanyCode, "Invalid corporation structure.");
    return;
  }

  var tokensetting = (from ts in _currentcorp.Tokens
                      where ts.CorporationStructureId == corpstructure.CorporationStructureId                           
                      select ts.TokenSettings).FirstOrDefault();

tokensetting is coming back as ienumerable even though I'm telling it to get firstordefault. 即使我告诉它要获得firstordefault,tokensetting又会变得无数。 I'm assuming that it's saying get firstordefault on the tokens? 我假设这是说在令牌上获得firstordefault?

Anyhow, there should only be 1 record in the tokensetting table even though the table is 1 to many. 无论如何,即使令牌设置表是一对多表,令牌设置表中也应该只有1条记录。 How do I return the firstordefault only on this table? 如何仅在此表上返回firstordefault?

==== ANSWER ==== ====答案====

As suggested, I did the following: 按照建议,我做了以下工作:

  var tokensetting = (from ts in _currentcorp.Tokens
                      where ts.CorporationStructureId == corpstructure.CorporationStructureId                           
                      select ts.TokenSettings.FirstOrDefault()).FirstOrDefault();

I believe the LINQ query is returning a IEnumerable of IEnumerables , so a second .FirstOrDefault() should get you what you want. 我相信LINQ查询返回的是IEnumerableIEnumerables ,因此第二个.FirstOrDefault()应该会为您提供所需的内容。

The first call specifies that you want the first row in the result of the LINQ query, and the second specifies that you want the first element of ts.TokenSettings , which is itself a collection. 第一个调用指定您想要LINQ查询结果中的第一行,第二个调用指定您想要ts.TokenSettings的第一个元素,该元素本身就是一个集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM