2

I have a domain entity called "HoldItems" and i created a list of HoldItems object.

All the attribute on that HoldItems are:

public int TransactionNo;
public int TransactionID;
public int ItemNo;
public String ItemName;
public int Qty;
public double Price;
public double TotalPrice;
public double DiscountPrice;
public int ItemType;
public bool VatInclude;
public double VatPrice;
public string Note;
public List<HoldExtraMenu> ExtraMenuList;
public string BelongCategoryName;
public int BelongItemNo;
public int BelongItemIndex;
public int BelongCategoryID;

I wrote some LINQ query like this, that selectedItemList represent List<DomainEntities.HoldItems>

var results = from myobject in selectedItemList
              where myobject.ItemType == 5
              where myobject.BelongItemIndex==selectedItemList.IndexOf(C)                                      
              orderby myobject.BelongCategoryName                                                                            
              select myobject;

Now the question is, I need to group this result by BelongCategoryName and concatenate output column for ItemName using add ","

1 Answer 1

3

You can achieve the requirement as follows:

var resultSet = 
       results.GroupBy(e => e.BelongCategoryName)
              .ToDictionary(e => e.Key, 
                  g => string.Join(",", g.Select(a => a.ItemName)));

resultSet is now a Dictionary<string, string>.

or if you want to chain the above query to the one you've already started:

var results = (from myobject in selectedItemList
               where myobject.ItemType == 5
               where myobject.BelongItemIndex == selectedItemList.IndexOf(C)
               orderby myobject.BelongCategoryName
               group myobject by myobject.BelongCategoryName)
               .ToDictionary(e => e.Key,
                      g => string.Join(",", g.Select(a => a.ItemName)));

or using query syntax only:

var results = from myobject in selectedItemList
              where myobject.ItemType == 5
              where myobject.BelongItemIndex == selectedItemList.IndexOf(C)
              orderby myobject.BelongCategoryName
              group myobject by myobject.BelongCategoryName into h
              select new
              {
                   BelongCategoryName = h.Key,
                   ItemNames = string.Join(", ", from e in h select e.ItemName)
              };
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much for your help. It's working fine.
@SithijaSahan you're welcome. I've also provided a query syntax only version if you're not a fan of the method syntax.
I need some little help, Meantime can i get summation of ItemPrice? Thanks
@SithijaSahan sure... do you want a Dictionary<string, int> or an anonymous type with two properties of BelongCategoryName and sum of the price?
2nd way is better, getting two properties, I checked you last code block, it's working fine and output is showing like "BelongCategoryName" & "ItemNames" i think it's better to have this type of output , Eg: BelongCategoryName,ItemNames,TotalPrice Like this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.