Group by LINQ vs group by SQL - c#

I have the following query in SQL which I would like to convert to LINQ statement.
select AreaId, BrandId, MilestoneId, DocumentCategoryId
from Document
group by AreaId, Brandid, MilestoneId, DocumentCategoryId
I have tried, e.g.,
var docs =
from d in documents
group d by new
{
d.Area,
d.Brand,
d.MilestoneId,
d.DocumentCategoryId
} into gcs
select new Group()
{
Area = gcs.Key.Area,
Brand = gcs.Key.Brand,
MilestoneId = gcs.Key.MilestoneId,
DocumentCategoryId = gcs.Key.DocumentCategoryId,
};
And
var docs = documents
.GroupBy(d => new Group
{
Area = d.Area,
Brand = d.Brand,
MilestoneId = d.MilestoneId,
DocumentCategoryId = d.DocumentCategoryId,
})
but the result in SQL returns 88 rows (the aim), in query syntax 78 rows and in LINQ 270 (total number).
I would like a LINQ statement for to return 88 rows.

I expect the final version is essentially switching to LINQ-to-Objects - i.e. populating a Group object per row, then grouping in .NET terms, which will mean one row per group as I doubt your Group implements the right patterns for equality. To get LINQ to treat the last version correctly, you probably need to add an anonymous type into the mix (LINQ understands this should behave like a tuple):
var docs = documents
.GroupBy(d => new
{
Area = d.Area,
Brand = d.Brand,
MilestoneId = d.MilestoneId,
DocumentCategoryId = d.DocumentCategoryId,
}).Select(grp => new Group {
Area = grp.Key.Area,
Brand = grp.Key.Brand,
MilestoneId = grp.Key.MilestoneId,
DocumentCategoryId = grp.Key.DocumentCategoryId,
});
As for the 88 vs 78: have you looked at what SQL is being issued? That should tell you what it is doing differently.

Related

LINQ query with a where condition containing

I am just learning LINQ and I have come across and issue Im not sure how to do in LINQ.
string numbers = "1,3,4,5";
string[] outletsInaStringArray = outlets.Split(',');
List<string> numbersAsAList = outletsInaStringArray.ToList();
I have a field in my database which holds a number. I only want to select the lines WHERE the number in the database is IN the line list of numbers "1,3,4,5" (these numbers are just examples).
Thanks in advance
I have looked at Tim and James answers and also looked at the line that James has sent. Im still a bit confused.....Sorry. Below is my actual code. It compiles but does not work
string outlets = "1,3,4,5"
string[] outletsNeeded = outlets.Split(',');
List<string> outletsNeededList = outletsNeeded.ToList();
DashboardEntities1 db = new DashboardEntities1();
var deptSalesQuery = (
from d in db.DashboardFigures
where (d.TypeOfinformation == "DEPTSALES") && (outletsNeeded.ToString().Contains(d.OutletNo.ToString()))
select new DeptSales
{
Dn = (int)d.Number,
Dnm = "Mens",
On = d.OutletNo,
Qs = (double)d.Value_4,
Se = (double)d.Value_2,
Si = (double)d.Value_3
}
);
In the DASHBAORDFIGURES table in SQL I have 2 records where the outlets number = 1, and therefore should have come up with two records.
Sorry if this is a simple thing, its just new to me and its frustrating.
You can use Contains as tagged:
var query = db.Table
.Where(x => outletsInaStringArray.Contains(x.Number) && x.information == "SALES");
that was method syntax, if you prefer query syntax:
var query = from figure in db.Figures
where outletsInaStringArray.Contains(figure.number)
&& figure.information == "SALES"
select figure;
But the column number is int, the List<string> stores strings, maybe your LINQ provider does not support .Contains(figure.ToString()). Then convert the strings to int first:
List<int> outletsNeededList = outletsNeeded.Select(int.Parse).ToList();
The answer that Tim provided is one method. Linq and lambda are interchangeable. Have a look at the following posting as well. Link
var result = from x in db.Table.ToList()
where outletsInaStringArray.Contains(x.Number)
select x;
Also have a look the following as it offers a very similar solution to the one you are looking for:
Link
As per i understand, you want to fetch data in similar way as IN (SQL) clause does it.
SELECT <Field_List>
FROM Table
WHERE IntegerField IN (1,2,4,5)
But i'm wondering why do you want to do it that way, when you can join data and get only matches. The worse is that you're trying to mix different data type and pass comma delimited text as a set of integers (i may be wrong):
SELECT <Field_List>
FROM Table
WHERE IntegerField IN ("1,2,4,5")
Above query won't execute, because the set of integers is "packed" into comma delimited string. To be able to execute that query, a conversion between data types must be done. Numbers in a string have to be converted to a set of integers (using user define split function or Common Table Expression):
;WITH CTE AS
(
--here convertion occurs
)
SELECT t2.<Field_List>
FROM CTE As t1 INNER JOIN TableName AS t2 ON t1.MyNumber = t2.IntegerField
Linq + any programming language is more flexible. You can build a list of integers (List) to build query.
See simple example:
void Main()
{
List<MyData> data = new List<MyData>{
new MyData(1,10),
new MyData(2, 11),
new MyData(5, 12),
new MyData(8, 13),
new MyData(12, 14)
};
//you're using comma delimited string
//string searchedNumbers = "1,3,4,5";
//var qry = from n in data
// join s in searchedNumbers.Split(',').Select(x=>int.Parse(x)) on n.ID equals s
// select n;
//qry.Dump();
List<int> searchedNumbers = new List<int>{1,2,4,5};
var qry = from n in data
join s in searchedNumbers on n.ID equals s
select n;
qry.Dump();
}
// Define other methods and classes here
class MyData
{
private int id = 0;
private int weight = 0;
public MyData(int _id, int _weight)
{
id = _id;
weight = _weight;
}
public int ID
{
get{return id;}
set {id = value;}
}
public int Weight
{
get{return weight;}
set {weight = value;}
}
}
Result:
ID Weight
1 10
5 12
Cheers
Maciej
Thank you all iv now got it to work using all your suggestions
the final code that works is as follows
DeptSales myDeptSales = new DeptSales(); // Single department
List<DeptSales> myDeptSalesList = new List<DeptSales>(); // List of Departments
DashboardEntities1 db = new DashboardEntities1();
var deptSalesQuery = from d in db.DashboardFigures
join s in outlets.Split(',').Select(x => int.Parse(x)) on d.OutletNo equals s
where (d.TypeOfinformation == "DEPTSALES")
select new DeptSales
{
Dn = (int)d.Number,
Dnm = "Mens",
On = d.OutletNo,
Qs = (double)d.Value_4,
Se = (double)d.Value_2,
Si = (double)d.Value_3
};
Thanks once again.

Concatenate ids based off of same Name in Entity Framework/Linq

Given a list of states that have location ids:
location_id name
----------- ----
1546 Arizona
8543 Arizona
7894 Arizona
8456 Maine
8354 New York
1268 New York
I am selecting from this table as such
var query = (from s in db.Locations
//A bunch of joins and where clause to filter list
select new { s.location_id, s.name });
I would like to get a list that contains
location_id name
----------- ----
1546,8543,7894 Arizona
8456 Maine
8354,1268 New York
How would I go about this?
I read that entity framework can't translate String.Join so I would have to call ToList() first and then select from that list joining the location ids but when I do it I get the same list that I started with.
How can I get the result I am looking for?
Thank you.
Just group:
var query2 = from l in query.AsEnumerable()
group l by l.name into g
select new {
location_id = String.Join(",", g.Select(x=>x.location_id.ToString())),
name = g.Key
};
I believe you'll need the AsEnumerable() call because you cannot translate a String.Join into SQL. You can of course ToList() instead if you prefer to eagerly load. However, as #Servy points out, you should do the grouping on the database side:
var query2 = from g in query.GroupBy(l => l.name).AsEnumerable()
select new {
location_id = String.Join(",", g.Select(x=>x.location_id.ToString())),
name = g.Key
};
Essentially all you're doing here is a GroupBy. You can then manipulate the results of the group in linq to objects, rather than the query, after you've pulled the results:
var dbquery = (from s in db.Locations
//A bunch of joins and where clause to filter list
group s.location_id by s.name into locations
select new { locations, name = locations.Key });
var query = dbquery.AsEnumerable()
.Select(group => new
{
name = group.name,
locations = string.Join(",", group.locations)
});

C# LINQ Query

I have some data in a List of User defined types that contains the following data:
name, study, group, result, date. Now I want to obtain the name, study and group and then a calculation based onthe result and date. The calculation is effectively:
log(result).where max(date) minus log(result).where min(date)
There are only two dates for each name/study/group, so the result from the maximum data (log) minus the result from the minumum date (log). here is what I have tried so far with no luck:
var result =
from results in sortedData.AsEnumerable()
group results by results.animal
into grp
select new
{
animal = results.animal,
study = results.study,
groupNumber = results.groupNumber,
TGI = System.Math.Log(grp.Select(c => c.volume)
.Where(grp.Max(c=>c.operationDate)))
- System.Math.Log(grp.Select(c => c.volume)
.Where(grp.Min(c => c.operationDate)))
};
Anybody any pointers? Thanks.
It isn't entirely clear how the grouping relates to your problem (what sense does it make to extract a property from a range variable after it has been grouped?), but the part you're having difficult with can be solved easily with MaxBy and MinBy operators, such as the ones that come with morelinq.
var result = from results in sortedData.AsEnumerable()
group results by results.animal into grp
select new
{
animal = grp.Key,
study = ??,
groupNumber = ??,
TGI = Math.Log(grp.MaxBy(c => c.operationDate).volume)
- Math.Log(grp.MinBy(c => c.operationDate).volume)
};
Otherwise, you can simulate these operators with Aggregate, or if you don't mind the inefficiency of sorting:
var result = from results in sortedData.AsEnumerable()
group results by results.animal into grp
let sortedGrp = grp.OrderBy(c => c.operationDate)
.ToList()
select new
{
animal = grp.Key,
study = ??,
groupNumber = ??,
TGI = sortedGrp.Last().volume - sortedGrp.First().volume
};
You have a few syntax problems, you cannot use the results parameter after your into grp line. So my initial attempt would be to change your statement like so
var result =
from results in sortedData.AsEnumerable()
group results by new
{
Animal = results.animal,
Study = results.study,
GroupNumber = results.groupNumber
}
into grp
select new
{
animal = grp.Key.Animal,
study = grp.Key.Study,
groupNumber = grp.Key.GroupNumber,
TGI = System.Math.Log(grp.OrderByDescending(c=>c.operationDate).First().volume)
- System.Math.Log(grp.OrderBy(c=>c.operationDate).First().volume)
};

LINQ group by month question

I'm new to LINQ to SQL and I would like to know how to achieve something like this in LINQ:
Month Hires Terminations
Jan 5 7
Feb 8 8
Marc 8 5
I've got this so far, and I think there is something wrong with it but I'm not sure:
from term1 in HRSystemDB.Terminations
group term1 by new { term1.TerminationDate.Month, term1.TerminationDate.Year } into grpTerm
select new HiresVsTerminationsQuery
{
Date = Criteria.Period,
TerminationsCount = grpTerm.Count(term => term.TerminationDate.Month == Criteria.Period.Value.Month),
HiresCount = (from emp in HRSystemDB.Persons.OfType<Employee>()
group emp by new { emp.HireDate.Month, emp.HireDate.Year } into grpEmp
select grpEmp).Count(e => e.Key.Month == Criteria.Period.Value.Month)
});
Thanks in advance.
I'm not quite sure where does the Criteria.Period value come from in your sample query.
However I think you're trying to read both hires and terminations for all available months (and then you can easily filter it). Your query could go wrong if the first table (Termination) didn't include any records for some specified month (say May). Then the select clause wouldn't be called with "May" as the parameter at all and even if you had some data in the second table (representing Hires), then you wouldn't be able to find it.
This can be elegantly solved using the Concat method (see MSDN samples). You could select all termniations and all hires (into a data structure of some type) and then group all the data by month:
var terms = from t in HRSystemDB.Terminations
select new { Month = t.TerminationDate.Month,
Year = term1.TerminationDate.Year,
IsHire = false };
var hires = from emp in HRSystemDB.Persons.OfType<Employee>()
select new { Month = emp.HireDate.Month,
Year = emp.HireDate.Year
IsHire = true };
// Now we can merge the two inputs into one
var summary = terms.Concat(hires);
// And group the data using month or year
var res = from s in summary
group s by new { s.Year, s.Month } into g
select new { Period = g.Key,
Hires = g.Count(info => info.IsHire),
Terminations = g.Count(info => !info.IsHire) }
When looking at the code now, I'm pretty sure there is some shorter way to write this. On the other hand, this code should be quite readable, which is a benefit. Also note that it doesn't matter that we split the code into a couple of sub-queries. Thanks to lazy evalutation of LINQ to SQL, this should be executed as a single query.
I don't know if it shorter but you can also try this version to see if it works better with your server. I don't know exactly how these two answers turn into SQL statements. One might be better based on your indexs and such.
var terms =
from t in Terminations
group t by new {t.Month, t.Year} into g
select new {g.Key, Count = g.Count()};
var hires =
from p in Persons
group p by new {p.Month, p.Year} into g
select new {g.Key, Count = g.Count()};
var summary =
from t in terms
join h in hires on t.Key equals h.Key
select new {t.Key.Month, t.Key.Year,
Hires = h.Count, Terms = t.Count};

Child collection not filtering even though linq to sql join exists

I have a LINQ query:
var result = from mt in MessageTypes
join mtfmt in MessageTypeField_MessageTypes
on new { MessageTypeID = mt.ID, MessageTypeFieldID = messageTypeFieldId } equals new { MessageTypeID = mtfmt.MessageTypeID, MessageTypeFieldID = mtfmt.MessageTypeFieldID }
where (mt.StatusID == (int)status)
select mt;
Or Lambda syntax if you prefer (the one I am using) (messageTypeFieldID is set above the var call from a param.):
var messageTypes = context.MessageTypes
.Join(
context.MessageTypeField_MessageTypes,
mt =>
new
{
MessageTypeID = mt.ID,
MessageTypeFieldID = messageTypeFieldID
},
mtfmt =>
new
{
MessageTypeID = mtfmt.MessageTypeID,
MessageTypeFieldID = mtfmt.MessageTypeFieldID
},
(mt, mtfmt) =>
new
{
mt = mt,
mtfmt = mtfmt
}
)
.Where(x => (x.mt.StatusID == (int)status))
.Select(x => x.mt);
I've just started learning LINQ joins and approached a situation where it is required in a new normalization (many-many) table I am setting up and I wish to return all message types each with their associated field which lives under 'MessageTypeField_MessageType'.
My 'MessageTypeField_MessageTypes' table is a fairly simple normalization strategy setup like this and I should state even though MessageTypeID 'has many' it is unique data so it could be:
[ID | MessageTypeID | MessageTypeFieldID]
1 63 10
1 63 11
1 63 12
Now the above code executes and returns a query I am happy with.. (performs correct INNER JOIN) but when I look at messageTypes.ToList()[0].MessageTypeField_MessageTypes for example with quick watch, where I would expect to see 1 record, I am getting an entire stack of messageTypeField_MessageType records ~17, only filtered by MessageTypeFieldID and not by MessageTypeID as well. It should filter by the message type id on each iteration and only return me just the one record. Any ideas on what I am doing wrong, or how I can achieve what I need?
Sorry for the complicated example, but I hope you guys can help!
If you are looking at a navigation property (i.e. some child collection of mt), then this is unrelated to your query; it sounds like you want to use AssociateWith. This largely replaces the join if you are just trying to filter the child data...
Consider changing your select to:
select new
{
MessageType = mt,
MessageField = mtfmt
}
As Marc mentioned, changing the query is unrelated to the navigation properties. Something similar to the above is probably what you intended.
If you want to stick with the navigation properties, you should use a combination of AssociateWith and LoadWith. The first to filter, and the later to make it an eager load (so you don't end with multiple round trips).

Categories

Resources