Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have an entity table by name "seats". Records in this table are like this :

Code
10A
10B
10C
10D
11A
11C
11D
11E
312A
312B... so on


Here, For Ex : 10A -> A is the Seat Id and 10 is the table to which the seat is attached, if its 312B then 312 is table Id and B is the seat id.

I need to get the distinct table names from the above collection along with the count of seats around that table, like this :

Code        Seats
10------------4
11------------4
312-----------2


How can I achieve this. Please suggest. Thank you in advance.

What I have tried:

C#
private class tables
{
    public String code { get; set; }
    public Int32 noseats { get; set; }
}

List<tables> lstTables = new List<tables>();
	
lstTables = (from tablrow in Context.seats.AsEnumerable()
            group tablrow by 
            new { tabcode = apputils.Left(tablrow.code, tablrow.code.Length - 1) } 
            into g
            select new tables { code = g.Key.tabcode, noseats = g.Count(g) }).ToList();


This returns an error,
Argument 2: cannot convert from 'System.Linq.IGrouping<<anonymous type: string tabcode>, db.seats>' to 'System.Func<db.seats, bool>'
Posted
Updated 11-Mar-19 11:18am
v5
Comments
[no name] 9-Mar-19 8:41am    
Obviously, if you format the data (into a table and seat) "before" you query it, things would be easier.
Priya-Kiko 11-Mar-19 1:27am    
Thank you for the response.

try this:
Dictionary<int, int> SeatsByTableID = lstTables
    .GroupBy(tbl => tbl.TableId)
    .ToDictionary(grp => grp.Key, grp => grp.Count());
Note that I find your naming incongruent: if the table id is the integer, why do you call it 'noseats ?

I assume here your Class should be:
private class Table
{
    public Table(int tableId, string seatcode)
    {
        this.TableId = tableId;
        this.SeatCode = seatcode;
    }

    public int TableId { get; set; }
    public String SeatCode { get; set; }
}
Update:

If you really want an IEnumerable;
C#
var IESeatsByTableID = lstTables
    .GroupBy(tbl => tbl.TableId)
    .Select(grp => new {TblId = grp.Key, NSeats = grp.Count()});
Keep in mind the result is a:

{System.Linq.Enumerable.WhereSelectEnumerableIterator<system.linq.igrouping<int, fourformui.table="">, <>f__AnonymousType0<int, int="">>}

And, it's up to you to remember the internal field names, 'TableId and 'NSeats, when you use the IEnumerable.
 
Share this answer
 
v2
Comments
Priya-Kiko 11-Mar-19 1:41am    
Thank you so much @BillWoodruff. Applying your suggestion I was able to achieve what I wanted exactly. Thanks again.

Well, 'noseats' denotes 'number of seats' against that table, for internal understanding.

I changed my code to :

Dictionary<string, int32=""> SeatsByTableID =
Context.seats.AsEnumerable()
.GroupBy(tbl => apputils.Left(tbl.code, tbl.code.Length - 1))
.ToDictionary(grp => grp.Key, grp => grp.Count());

Thank you for your time and help.
BillWoodruff 11-Mar-19 4:49am    
You're welcome. I've added an update to the post to show how to create an IEnumerable result.
Priya-Kiko 11-Mar-19 5:19am    
Noted. Thanks again.
Maciej Los 11-Mar-19 16:50pm    
5ed!
BillWoodruff 12-Mar-19 3:42am    
Thank you, Maciej
Assuming that Context.seats returns a list of string...

C#
string pattern = @"\d+";
List<tables> lstTables = Context.seats.ToList()
    .GroupBy(s=>Regex.Match(s, pattern).Value)
    .Select(grp=>new tables()
        {
            code = grp.Key,
            noseats = grp.Count()
        })
    .ToList();
 
Share this answer
 
v2
Comments
Priya-Kiko 12-Mar-19 0:46am    
Thank you for your time and suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900