Wednesday 15 June 2011

c# - using linq to get a single record from a table linked to another table -


This question is confusing because I do not know how to ask it.

Anyhow, I have 4 tables and I have to go to the tokensetting table to get a record. I have been given a codec and I have to travel from there and I need all the information. And because there are several corporate structures for each corporation, I must first obtain the structure of the corporation to be handed over to the token first.

Here's the layout:

Enter image details here < / P>

This is my code:

  _currentcorp = (from _entities.Comporations where c.CompanyCode == textBox_CompanyCode.Text selection C). FirstOrDefault (); If (_currentcorp == faucet) {errorProvider1.SetError (textBox_CompanyCode, "Invalid corporation."); Return; } Var corpstructure = (From the CS in _currentcorp. Corporation Structure where cs.District == zero & amp; cs.Branch == faucet & cs.Region == tap selection CS) .FirstOrDefault (); If (corpstructure == zero) {errorProvider1.SetError (textBox_CompanyCode, "Invalid corporation structure."); Return; } Var tokensetting = (From TS in _currentcorp.Tokens where ts.CorporationStructureId == corpstructure.CorporationStructureId ts.TokenSettings is selected) .FirstOrDefault ();   

The tokensetting is coming back in the form of ienumerable, even though I'm asking to get it first. I think it's saying that you meet first on tokens?

Anyhow, the tokenetting table should contain only 1 record, even if the table is many from 1 How do I return only this table for the first time?

==== Answer ====

As I have suggested, I have done the following:

 < Code> var tokensetting = (from TS in _currentcorp.T.CON) where ts.CorporationStructureId == corpstructure.CorporationStructureId is selected ts.TokenSettings.FirstOrDefault ()). FirstOrDefault ();    

I believe LINQ query is a IEnumerable Returning IEnumerables , then a second . First and last (default) you should get whatever you want.

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 the ts.TokenSettings , which is a The collection is.

No comments:

Post a Comment