Entity framework linq query - c#

I have this piece of code. What i need to do is to exclude mc_host_class values that are inside the list.
enter var myList = (from p in db.Full
where ( (p.date_reception > begin & p.date_reception < end & !p.mc_host_class.Contains("NULL")) &
( !p.mc_host_class.Contains( (
from p2 in db.exclure
where (p2.type.Contains("Host"))
group p2 by p2.libelle into g
select new { libellex = g.Key}).ToList()
)))
group p by p.mc_host_class into g
orderby g.Count() descending
select new
{
hostclassx = g.Key,
countx = g.Count()
}).ToList().Take(10);
Thank you for helping

If I understood your question, I think this could help you:
(!(
from p2 in db.exclure
where (p2.type.Contains("Host")
group p2 by p2.libelle into g
select new { libellex = g.Key}
).ToList().Contains(p.mc_host_class))
List1.Contains(value1) return a bool if a value1 is in List1, but you used it like value1.Contains(List1).

Related

Matrix output in ASP.NET MVC LINQ

I have 3 simple tables
tblQual (ID, QualName)
tblPerson (ID, PersonName)
tblPersonQual (ID, PersonID, QualID, ExpiryDate)
I would like to display a matrix with PersonName down the left, QualName at the top and ExpiryDate in the middle. How do I go about doing this?
I've tried this but no joy.
var QualMatrix = from c in db.tblPersonQual
join q in db.tblQual on c.QualID equals q.ID
join p in db.tblPerson on c.PersonID equals p.ID
group c by c.ID into g
select new
{
rowKey = g.Key,
rowData = g.Select(c => new { Qual = q.QualName, Expiry = c.Expiry })
};
In terms of output view something similar to this
As my understanding, you want to display a matrix with QualName, ExpiryDate and QualName. We can use the same query by just modifying group by clause.
var QualMatrix = from c in db.tblPersonQual
join q in db.tblQual on c.QualID equals q.ID
join p in db.tblPerson on c.PersonID equals p.ID
group new { q.QualName,p.PersonName} by new { c.ID,c.ExpiryDate } into g
select new
{
QualName=g.Select(e=>e.QualName).FirstOrDefault(),
ExpirtyDate=g.Key.Expiry,
PersonName=g.Select(e=>e.PersonName).FirstOrDefault(),
};
Hopefully, This will fulfil your requirement.

Entity framework average , group by query

Hello i have the following query
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end & !p.mc_object.Contains("NULL")
& (!strListe.Contains(p.mc_object)))
group p by new { p.mc_object} into g
select g.OrderByDescending(p => new {p.duration,p.mc_object} ) into r
select new StringIntType
{
str = r.mc_object,
nbr = r.duration.Value
}).Take(50).ToList();
I need to group by mc_object , and select mc_object , average of duration
and order by average of duration descending ,Thank you for helping
The query should be
var myList = (from p in db.Full
// Don't use &! Use &&
where p.date_reception > begin &&
p.date_reception < end &&
!p.mc_object.Contains("NULL") &&
!strListe.Contains(p.mc_object)
group p by p.mc_object into g
select new
{
mc_object = g.Key,
/* data = g, */
avg = g.Average(x => x.duration)
} into h
// if you want both descending, add descending after mc_object
orderby h.avg descending, h.mc_object
select new StringIntType
{
str = h.mc_object,
nbr = (int)h.avg // Average returns a double
}).Take(50).ToList();
}
Note that I've changed the & to &&. For the ordering I wasnt sure. I dont need the full data in the second half of the query, so I've added a commented data = g

Linq query Contains

i have an issue with this query
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end
& !p.mc_owner.Contains("NULL") & !p.mc_owner.Contains(""))
group p by p.mc_owner into g
orderby g.Count() descending
select new
{
Ownerx = g.Key,
countx = g.Count()
}).ToList();
Everything works well without adding !p.mc_owner.Contains(""), but once i do it just shows no results
Any string with value will contain empty string. For example next testcode will print true.
string a = "abc";
Console.WriteLine(a.Contains(""));
Console.ReadLine();
You probably meant to write:
p.mc_owner != ""
or it is better to write
!string.IsNullOrEmpty(p.mc_owner)
I guess with .contains("NULL") you wanted to leave out NULL values and not the string "NULL". Also I would && instead of &. I suggest to rewrite to:
(p.date_reception > begin && p.date_reception < end
&& !string.IsNullOrEmpty(p.mc_owner))
The way I would do this is:
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end
& !p.mc_owner.Contains("NULL") & !string.IsNullOrWhiteSpace(p.mc_owner))
group p by p.mc_owner into g
orderby g.Count() descending
select new
{
Ownerx = g.Key,
countx = g.Count()
}).ToList();

How can I filter results of one LINQ query based on another?

Given the following:
DP_DatabaseTableAdapters.EmployeeTableAdapter employeetableAdapter = new DP_DatabaseTableAdapters.EmployeeTableAdapter();
DP_Database.EmployeeDataTable employeeTable = employeetableAdapter.GetData();
var leadEmployees = from e in employeeTable
where e.IsLead == true
select e;
DP_DatabaseTableAdapters.LaborTicketTableAdapter tableAdapter = new DP_DatabaseTableAdapters.LaborTicketTableAdapter();
DP_Database.LaborTicketDataTable table = tableAdapter.GetDataByDate(date.ToString("MM/dd/yyyy"));
var totHours = from l in table
join e in leadEmployees on l.EmployeeID equals e.EmployeeID
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
Total hours correctly filters the results based on the leadEmployee's list of people who have the IsLead bit set to true.
I would like to know how to do this with a where clause, I have attempd to use leadEmployees.Contanis but it wants a whole EmployeeRow...
How can I add what looks to be part of an IN clause to a where filter to replace the join?
var totHours = from l in table
where ??????
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
The contains will only want a whole EmployeeRow if you are selecting whole employee roles. You can either:
leadEmployees.Select(e => e.id).contains
OR
leadEmployees.Count(e => e.id == l.id) > 0
Both will work. (Excuse slightly rushed lack of consideration for syntax accuracies).
This should work:
var leadEmployees = from e in employeeTable
where e.IsLead == true
select e.EmployeeID;
var totHours = from l in table
where leadEmployees.Contains(l.EmployeeID)
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};

Linq query with left join and group

I'm unable to convert this SQL query into a working linq statement
select sum(cena), id_auta, max(servis)
from dt_poruchy left outer join mt_auta on dt_poruchy.id_auta=mt_auta.id
where dt_poruchy.servis>=3 group by id_auta;
I tryed something like this but i cant handle the select statement
var auta = from a in MtAuta.FindAll()
join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
from ap2 in ap.DefaultIfEmpty()
where ap2.SERVIS >= 3
group ap2 by ap2.ID into grouped
select new {
I'll appreciate any help!
Based on the limited information provided (which tables are certain fields from?), here is what I came up with.
var auta = from a in MtAuta.FindAll()
let p = a.DtPoruchys.Where(s => s.SERVIS >= 3)
select new
{
Id = a.Id,
CenaSum = p.Sum(c => c.Cena),
Servis = p.Max(s => s.SERVIS)
};
I've reached this solution (supposing "cena" belongs to MtAuta.FindAll()):
var auta = from e in
(from a in DtPoruchy.FindAll()
where a.SERVIS >= 3
join p in MtAuta.FindAll() on a.MtAuta equals p.Id into ap
from ap2 in ap.DefaultIfEmpty()
select new
{
Cena = ap.cena,
IdAuta = a.MtAuta,
Servis = a.servis
})
group e by e.IdAuta into g
select new
{
Cena = g.Sum(e => e.cena),
IdAuta = g.Key,
Servis = g.Max(e => e.servis)
};
I am not sure which table cena and servis are coming from but to create grouped sum you do something like.
select new { Sum = grouped.Sum( x => x.cena ) }
and to get max
select new { Max = grouped.Group.Max( x => x.servis ) }
Here is a good reference for you.
MSDN - 101 LINQ Samples
I've modified your solution little bit and i got it working like this:
var auta = from jo in
(
from a in MtAuta.FindAll()
join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
from ap2 in ap.DefaultIfEmpty()
where ap2.SERVIS >= 3
select new
{
Cena = ap2.CENA,
Idauto = ap2.ID_AUTA,
Servis = ap2.SERVIS
}
)
group jo by jo.Idauto into g
select new
{
Cena = g.Sum(jo => jo.Cena),
IdAuto = g.Key,
Servis = g.Max(jo => jo.Servis)
};
I just curious if this is the best solution?

Categories

Resources