I have 3 tables:
sessions - This store information about trainings
xref_session_faculty - This cross references the trainings and the teacher
user - list of all teachers
In one(or more) LINQ query i want to get all the sessions and for each session the teachers that will be conducting the training. Each session can have zero or more teachers in the DB.
sessions = db.sessions
.Where(x => x.seminar_id == seminarId)
.ToList()
.Select((x, i) => new fees
{
id = x.id,
sessionTitle = x.title,
teacherNames = "By:" + String.Join(",",
x.xref_session_faculty.ToList()
.Select(q => db.users
.Where(m => m.id == q.user_id)
.Select(t => t.firstName).ToList()
)
)
})
.ToList();
With this the teacherNames prints out By:System.Collections.Generic.List1[System.String],System.Collections.Generic.List1[System.String].
WHat is the right query format here?
teacherNames = "By:" + String.Join(",",
x.xref_session_faculty.ToList()
.Select(q => db.users
.Where(m => m.id == q.user_id)
.Select(t => t.firstName).FirstOrDefault()
)
)
you need to change ToList to FirstOrDefault function to get correct result
private var sessions = (from session in db.sessions.Where(x => x.seminar_id == seminarId)
select new
{
id = session.id,
sessionTitle = session.title,
teacherNames = (from faculty in db.xref_session_faculty.
where (x => x.session_id == session.id)
join us in db.uses on faculty.user_id equals us.user_id
select new
{
us.firstName,
other_field_names
})
});
I have a table that holds 15 session bookings by dates. Each row is a persons db entry for the sessions they attend.
For each date I need to count the bools for each session S1, S2 etc
How many are in S1 for 1/1/2019?.
Here is the table in asp.net core
![Pic of the table][https://imgur.com/a/SguJmLI]
It looks like this
Session S1 S2 S3 S4 S5 S6 S7
1/1/2019 1 0 1 1 1 0 1
1/1/2019 0 1 1 1 1 1 1
2/1/2019 0 0 1 1 1 1 1
2/1/2019 0 1 1 1 1 1 1
There are multiple session dates, and I need to get a summary count of the bools for each date.
1/1/2019 1 1 2 2 2 1 2
2/1/2019 0 1 2 2 2 2 2
Sadly I havn't got a single thing to generate
But this is how I am generating the grid
return View(await _context.SeatBooking
.OrderByDescending(s => s.SeatDate)
.ToListAsync());
Thanks guys,
Just a quick update this seems to work for me, but its clunky
ViewData["CountSessions"] = _context.SeatBooking
// .Where(s => s.StudentEmail == _userManager.GetUserId(User))
.GroupBy(d => d.SeatDate)
.Select(s => new
{
SumS1 = s.Count(c => c.S1 == true),
SumS2 = s.Count(c => c.S2 == true),
SumS3 = s.Count(c => c.S3 == true),
SumS4 = s.Count(c => c.S4 == true),
SumS5 = s.Count(c => c.S5 == true),
SumS6 = s.Count(c => c.S6 == true),
SumS7 = s.Count(c => c.S7 == true),
SumS8 = s.Count(c => c.S8 == true),
SumS9 = s.Count(c => c.S9 == true),
SumS10 = s.Count(c => c.S10 == true)
}).ToListAsync();
Since the Types of columns are bool, the first to do is to convert the bool to int .
Func<bool, int> convertBoolToInt = flag => flag ? 1 : 0;
var query = _context.SeatBooking
.Select(s => new SeatBookingDto
{
SeatDate = s.SeatDate,
S1 = convertBoolToInt(s.S1),
S2 = convertBoolToInt(s.S2),
S3 = convertBoolToInt(s.S3),
S4 = convertBoolToInt(s.S4),
S5 = convertBoolToInt(s.S5),
S6 = convertBoolToInt(s.S6),
S7 = convertBoolToInt(s.S7),
});
And now we can group and calculate the sum of each column for each group:
query.GroupBy(m => m.SeatDate, m => m, (k, g) => new
{
Date = k,
S1= g.Sum(m=>m.S1),
S2= g.Sum(m=>m.S2),
S3= g.Sum(m=>m.S3),
S4= g.Sum(m=>m.S4),
S5= g.Sum(m=>m.S5),
S6= g.Sum(m=>m.S6),
S7= g.Sum(m=>m.S7),
})
If you're using the EFCore , the corresponding query and result are :
App> SELECT [s].[SeatDate], [s].[S1], [s].[S2], [s].[S3], [s].[S4], [s].[S5], [s].[S6], [s].[S7]
App> FROM [SeatBooking] AS [s]
App> ORDER BY [s].[SeatDate]
App> 1/1/2019 12:00:00 AM : 1,1,2,2,2,1,2
App> 2/1/2019 12:00:00 AM : 0,1,2,2,2,2,2
Note the LINQ expression g.sum() here are all evaluated locally which means not evaluated by database . It seems that the EFcore has not evolved to be able to translate the Aggregate expression (such as Sum) to SQL query that can be executed on server .
If you want to query objects in memory only , there's another way to do that (not supported by EFCore yet) :
query.GroupBy(m=>m.SeatDate,m=>m,(k,g)=>new {
Date = k,
Count = g.Aggregate((c,n)=>new SeatBookingDto{
SeatDate=c.SeatDate,
S1=c.S1+n.S1,
S2=c.S2+n.S2,
S3=c.S3+n.S3,
S4=c.S4+n.S4,
S5=c.S5+n.S5,
S6=c.S6+n.S6,
S7=c.S7+n.S7,
})
})
I think you can try this:
_context.SeatBooking.Select(i => i.Session).Distinct().Select(i =>
new
{
Session = i,
S1 = _context.SeatBooking.Count(j => j.Session == i && j.S1 == 1),
S2 = _context.SeatBooking.Count(j => j.Session == i && j.S2 == 1),
}
).OrderByDescending(s => s.Session).ToList();
I have the below query that gets Name and TotalPoints as follows:
var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(Convert.ToInt32(this.ShopInstanceID), 10000, false)
.Where(row => row.RecievedPoints != "n/a")
.GroupBy(row => new { row.Name })
.Select(g => new
{
TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) * (x.Weightage.ToString() == "0.00" ? 1 : Convert.ToDouble(x.Weightage))),
Name = g.Key.Name
})
select data).ToList();
I will have data like below:
TotalPoints Name
5 A
10 B
5 C
15 D
5 E
If we observe the above list 5 is most common. I have to fetch that value from "gradeData".
How can I get that?
var mostCommon = gradeData.GroupBy(x => x.TotalPoints)
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.First();
The query below gives you the most common point along with all of its associated names:
var result = gradeData.GroupBy(x => x.TotalPoints)
.OrderByDescending(x => x.Count())
.Select(g => new
{
TotalPoints = g.Key,
Names = g.Select(x => x.Name).ToList()
})
.First();
How can check following code to catch divided by zero error?
active = (g.Sum(x => x.Kullanim_Reaktif) / g.Sum(x => x.kullanim_T0)) * 100
I want something like this:
if(g.Sum(x => x.kullanim_T0) == 0)
{
return 1;
}
else
{
return g.Sum(x => x.kullanim_T0);
}
Can I write if-else in above linq code line (g.Sum(x => x.kullanim_T0))? And how?
Thanks.
active = (g.Sum(x => x.kullanim_T0) == 0 ? 1 : g.Sum(x => x.Kullanim_Reaktif) / g.Sum(x => x.kullanim_T0)) * 100
but more efficient to do:
var kSum = g.Sum(x => x.kullanim_T0);
active = (kSum == 0 ? 1 : g.Sum(x => x.Kullanim_Reaktif) / kSum ) * 100
foreach (lc_ShoppingCart sc in shQuery)
{
//Decrement the Product Table's Total Remaining row with the quantity
var ProductInventoryQuery = (from pr in db.lc_ProductInventories
join c in db.lc_ColorTables on pr.Color equals c.Color
join s in db.lc_SizeTables on pr.Size equals s.Size
where pr.ProductID == Convert.ToInt32(sc.ProductID)
where pr.Color == c.Color
where pr.Size == s.Size
select pr).First();
ProductInventoryQuery.Quantity = ProductInventoryQuery.Quantity - sc.Quantity;
}
Probably something like this:
var ProductInventoryQuery =
db.lc_ProductInventories.Where(w => w.ProductID == Convert.ToInt32(sc.ProductID))
.Join(db.lc_ColorTables, p => p.Color, ct => ct.Color, (p, ct) => new { ProdInv = p, ColorTables = ct })
.Join(db.lc_SizeTables, p => p.Size, st => st.Color, (p, st) => new { ProdInv = p, SizeTables = st })
.First();