0

I have the following line in a WebApi controller;

string Codes = i.Products.FirstOrDefault().Code

As the line states, it gets the code, from the first Product.

But, what I really want it to do is to get all unique codes, and return them as a comma separated string.

So, let's say, there are 6 related products, and they have the following codes:

  • 45
  • 54
  • 45
  • 120
  • 54
  • 45

Right now, the statement just returns "45", given the above data.

But I want the statement above to return "45, 54, 120" (as a string).

How do I do this?

Complete code:

public System.Data.Entity.DbSet<WebAPI.Models.Product> Products { get; set; }
private ApplicationDbContext db = new ApplicationDbContext();

var product = await db.Products.Select(i =>
                new ProductDTO()
                {
                    Id = i.Id,
                    Created = i.Created,
                    Title = i.Title,                    
                    Codes = i.Products.FirstOrDefault().Code

                }).SingleOrDefaultAsync(i => i.Id == id);
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
brother
  • 7,651
  • 9
  • 34
  • 58
  • Do you need your controller to return a collection of codes or a string concatenation of these codes? – vc 74 Mar 16 '16 at 13:58

2 Answers2

0

I think Distinctand String.Join works for you.Please try this:

var product = await db.Products.AsEnumerable()//Turn AsEnumarable
.Select(i =>
   new ProductDTO()
   {
     Id = i.Id,
     Created = i.Created,
     Title = i.Title,                    
     Codes = string.Join(",", 
               i.Products.Select(l => l.Code).Distinct())
   }).SingleOrDefaultAsync(i => i.Id == id);
0

To convert to a comma separated list:

IEnumerable<string> distinctCodes = i.Products.Select(product => product.Code).Distinct();

return string.Join(",", distinctCodes);

But it's probably better if your controller returns a collection of string instead of a concatenated string.

Edit, after OP code update:

var DBProduct = await db.Products.SingleOrDefaultAsync(i => i.Id == id);

IEnumerable<string> productCodes = DBProduct.Products.Select(p => p.Code).Distinct();

var product = new ProductDTO()
{
   Id = DBProduct.Id,
   Created = DBProduct.Created,
   Title = DBProduct.Title,                    
   Codes = string.Join(",", productCodes)
};
vc 74
  • 37,131
  • 7
  • 73
  • 89
  • I updated my question, with more of the code. I am unsure how i would incorporate your answer, with my code above? – brother Mar 16 '16 at 14:12
  • I get this error 'LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' method, and this method cannot be translated into a store expression.' – brother Mar 16 '16 at 14:22
  • You must turn AsEnumerable before string.join look this [example](http://stackoverflow.com/questions/10079990/linq-to-entities-does-not-recognize-the-method-system-string-formatsystem-stri) – Alper Tunga Arslan Mar 16 '16 at 14:30
  • I now get 'IEnumerable does not contain a definition for SingleOrDefaultAsync'.. :/ – brother Mar 16 '16 at 15:58