Display fields with the same GUIDs together [duplicate]

Display fields with the same GUIDs together [duplicate]



This question already has an answer here:



I have a list of type ProductDetailDTO.


List<ProductDetailDTO> productDTOs;

public class ProductDetailDTO

public int ProductId get; set;
public string Name get; set;
public string Category get; set;
public byte Image get; set;
public string Description get; set;
public string Brand get; set;
public string GUID get; set;
public string VariantName get; set;
public string VariantValue get; set;
public decimal Price get; set;



I have used linq to bind the data to the list.


var productDetails = (from product in ekartEntities.Products
join productImage in ekartEntities.ProductImages on product.ProductId equals productImage.ProductId
join category in ekartEntities.ProductCategories on product.Category equals category.CategoryId
join mapping in ekartEntities.ProductVariantMappings on product.ProductId equals mapping.ProductId
join variant in ekartEntities.ProductVariants on mapping.ProductVariantId equals variant.ProductVariantId
join inventory in ekartEntities.Inventories on mapping.GUID equals inventory.Guid
where product.ProductId == productDetailDTO.ProductId
select new ProductDetailDTO()

ProductId = product.ProductId,
Name = product.Name,
Category = category.Name,
Description = product.Description,
Brand = product.Brand,
Image = productImage.Image,
GUID = mapping.GUID.ToString(),
VariantName = variant.Name,
VariantValue = mapping.Value,
Price = inventory.Price
).ToList();



Now, I want to display all the variants (VariantName and VariantValue) with the same GUIDs together. How can I achieve that?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






how did you bind data to list?

– Udara Kasun
Sep 10 '18 at 6:48






What exactly do you mean by "display fields together"?

– Lasse Vågsæther Karlsen
Sep 10 '18 at 6:49







@UdaraKasun Using linq. Updated the post!

– Suyash Gupta
Sep 10 '18 at 6:49






@LasseVågsætherKarlsen I want to display the columns of the list based on a column of the same list.

– Suyash Gupta
Sep 10 '18 at 6:52






did you try to group by GUI

– Udara Kasun
Sep 10 '18 at 6:53




2 Answers
2



You can use GroupBy and Select like this:


var variants = productDTOs
.GroupBy(k => k.GUID)
.Select(v => v
.Select(variant => new

variant.VariantName,
variant.VariantValue
));






Can I combine this variants with my existing productDTOs list? Or can I do the grouping at the time of retrieving the other values from the db in productDTOs?

– Suyash Gupta
Sep 10 '18 at 8:07







You can do both.

– Bizhan
Sep 10 '18 at 14:26



You can use Group By


group p by p.GUID into g
select new { Id = g.Key, ProductDetail = g.ToList()).ToList();



If you have tables before group by, then you can add new object in the group itself


group new p.xyz, n.xyz
by new p.GUID into g



otherwise use let to save the intermediate object in an object and do grouping on it

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)