First Last
First Last

Reputation: 59

In C#, how do I get a distinct value from an IEnumerable using LINQ?

I have an IEnumerable variable that I want to extract a distinct value from. I know all the entries in the rows of the list have the same value, I just need to get that value.

The method returns an IEnumerable.

The row in the IEnumerable is defined as: QuoteCovId AdditionalInterestId AdditionalInterestsAffiliateId AdditionalInterestsLastName AdditionalInterestsBusinessAddrLine1 AdditionalInterestsBusinessCity AdditionalInterestsBusinessState AdditionalInterestsBusinessZip

Sampel of code:

          IadditionalInterestData = AdditionalInterestData.GetAdditionalInterests(MasterPkgID, Requestor);

          // Using linq.
          var quotes = from ai in IadditionalInterestData
                       select Distinct(ai.QuoteCovId);

          // Iterate thru to get the 1 value.
          foreach (int QuoteCovId in quotes)
          {
              quoteID = QuoteCovId;
          }

Upvotes: 0

Views: 622

Answers (2)

Dan
Dan

Reputation: 11

But that method:

AdditionalInterestData.GetAdditionalInterests(MasterPkgID, Requestor); returns me an IEnumerable which I will use further in my application. Which is what I need.

So how will your suggestion still give me that IEnumerable and give me the quote value which happens to be the same in the collection?

var quoteId = AdditionalInterestData.GetAdditionalInterests(MasterPkgID, Requestor).FirstOrDefault().Select(f => f.QuoteCovId);

Also, I just added your line of code as is and I get an error statement.

Upvotes: 1

bluetoft
bluetoft

Reputation: 5443

    var quoteId = AdditionalInterestData.GetAdditionalInterests(MasterPkgID, Requestor)
                  .FirstOrDefault().Select(f => f.QuoteCovId);

Upvotes: 2

Related Questions