giligatech
asked on
Count with LINQ
Hi all,
I'm trying to implement LINQ query on some tables
I have a table called "Raffles", "Orders", "OrderTickets":
Raffles:
-------------------
-ID
-Name
-.......
Orders:
-----------------
-ID
-RaffleID
-Enabled (bit)
-.........
OrderTickets:
-------------------
-ID
-OrderID
-TicketNumber (int)
-Enabled(bit)
I want to get the count of all tickets for the raffle - As long as Enabled = true (in orders and in orderTickets as well).
This is what I did:
var topRaffles = from rf in db.Raffles
select new
{
rf.ID,
rf.Name,
TicketsBought = rf.Orders.Count(p1 => p1.Enabled)
};
The problem is that now it's the count for all the orders that have the same RaffleID and Enabled = true. But I want also that OrderTickets.Enabled = true.
I tried doing something like:
var topRaffles = from rf in db.Raffles
select new
{
rf.ID,
rf.Name,
TicketsBought = rf.Orders.Count(p1 => p1.Enabled && p1 => p1.OrderTickets.Enabled)
};
I also tried:
var topRaffles = from rf in db.Raffles
select new
{
rf.ID,
rf.Name,
TicketsBought = (from or in db.Orders where or.Enabled == true && or.RaffleID == rf.ID select or.OrderTickets.Count(p => p.Enabled))
};
but it doesn't work.
What am I doing wrong?
I'm trying to implement LINQ query on some tables
I have a table called "Raffles", "Orders", "OrderTickets":
Raffles:
-------------------
-ID
-Name
-.......
Orders:
-----------------
-ID
-RaffleID
-Enabled (bit)
-.........
OrderTickets:
-------------------
-ID
-OrderID
-TicketNumber (int)
-Enabled(bit)
I want to get the count of all tickets for the raffle - As long as Enabled = true (in orders and in orderTickets as well).
This is what I did:
var topRaffles = from rf in db.Raffles
select new
{
rf.ID,
rf.Name,
TicketsBought = rf.Orders.Count(p1 => p1.Enabled)
};
The problem is that now it's the count for all the orders that have the same RaffleID and Enabled = true. But I want also that OrderTickets.Enabled = true.
I tried doing something like:
var topRaffles = from rf in db.Raffles
select new
{
rf.ID,
rf.Name,
TicketsBought = rf.Orders.Count(p1 => p1.Enabled && p1 => p1.OrderTickets.Enabled)
};
I also tried:
var topRaffles = from rf in db.Raffles
select new
{
rf.ID,
rf.Name,
TicketsBought = (from or in db.Orders where or.Enabled == true && or.RaffleID == rf.ID select or.OrderTickets.Count(p => p.Enabled))
};
but it doesn't work.
What am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi giligatech;
In the original question it looked like you what to know how many tickets each person got. The code snippet below does that.
Fernando
In the original question it looked like you what to know how many tickets each person got. The code snippet below does that.
Fernando
RafflesDataContext db = new RafflesDataContext();
var topRaffles = from OrderTicket in db.OrderTickets
where OrderTicket.Enabled == true && OrderTicket.Orders.Enabled == true
group OrderTicket by OrderTicket.Orders.Raffles.ID.ToString() + "|" + OrderTicket.Orders.Raffles.Name;
foreach (var grouping in topRaffles)
{
int ticketCount = 0;
String[] Key = grouping.Key.Split(new Char[] {'|'});
Console.Write("Raffle ID = {0} - Name = {1} - ", Key[0], Key[1]);
foreach( var tCount in grouping )
{
ticketCount += (int) tCount.Count;
}
Console.WriteLine(" TicketsBought = " + ticketCount);
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
As novynov said, there are many ways to the the linq query.
Thank you for your support.
Thank you for your support.
ASKER
tell me if you have a better solution.
Open in new window