I have a string field in database that keeps numbers as string. when I want to order based on this field in Linq as below but orderby does not work well. it orders like this
1 - 2 - 3 - 25 - 11 - 30 - 50 ===> 1 - 11 - 2 - 25 - 3 - 30 - 50
IQueryable<Tbl_Melk> Melks =
from melk in Tbl_Melk
where melk.Mantaghe == Mantaghe && melk.Hoze == Hoze && melk.Block == Block
orderby melk.Melk
select melk;
What says Maurice is right, You may try this:
var Melks = from melk in Tbl_Melk.ToList()
let integer = int.Parse(melk)
orderby integer
where melk.Mantaghe == Mantaghe && melk.Hoze == Hoze && melk.Block == Block
orderby melk.Melk
select melk;
But with that you are losing performance and the IQuerable interface.
Actually, that is the correct order because it's sorting the values in alphabetic order. LINQ cannot tell that the data contains numbers unless you coerce it into a numeric type. Check out this question for one way to sort the way you want.
Linq - Order by number then letters
LeftPad the string with zeros.
Out of my brain and to the lack of EntityFramework not supporting LeftPad:
....
orderby SqlFunctions.Replicate("0", 10 - melk.Melk.Length) + melk.Melk
Related
Does anyone have any tips for calculating percentages in Linq to Entities?
I'm guessing that there must be a more efficient way than returning 2 results and calculating in memory. Perhaps an inventive use of let or into?
EDIT
Thanks Mark for your comment, here is a code snippet, but I think this will result in 2 database hits:
int passed = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask)
where lpt.OnlineCourseScores.Any(score => score.ActualScore >= ((lpt.LearningResource.PassMarkPercentage != (decimal?)null) ?lpt.LearningResource.PassMarkPercentage : 80))
select lpt).Count();
int total = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask)
select lpt).Count();
double percentage = passed * 100 / total;
If you use LINQ to Entities and write something along the lines of select x * 100.0 / y in your query then this expression will be converted to SQL and run in the database. It will be efficient.
If I want to generate the query (month(created) = 1 and year(created) = 2010) or (month(modified) = 1 and year(modified) = 2010) with linq, how would I go about it?
I have o.Created.Value.Month == month && o.Created.Value.Year == year. If I do (o.Created.Value.Month == month && o.Created.Value.Year == year) || (o.Modified.Value.Month == month && o.Modified.Value.Year == year) wouldn't the parenthesis just be ignored?
No, the parentheses won't be ignored by LINQ - they're important to indicate the logic. They're effectively present in the expression tree, in that you'll end up with an "OR" expression with two subexpressions each of which is an "AND" expression.
The query you've given should be fine - have you tried it, and checked the resulting SQL?
I have a query that's running slow (in a loop of about 100 it takes 5-10 seconds) and have no clue why. It's simply querying against a List of objects... your help is much appreciated!
I'm basically querying for Schedules that have been assigned to specific managers. It must be from the specified Shifts week OR the first 2 days of next week OR the last 2 days of the previous week.
I tried calculating .AddDays before but that didn't help. When I ran a performance test it highlighted the "from" statement below.
List<Schedule> _schedule = Schedule.GetAll();
List<Shift> _shifts = Shift.GetAll();
// Then later...
List<Schedule> filteredSchedule = (from sch in _schedule
from s in _shifts
where
**sch.ShiftID == s.ShiftID
& (sch.ManagerID == 1 | sch.ManagerID == 2 | sch.ManagerID == 3)
& ((s.ScheduleWeek == shift.ScheduleWeek)
| (s.ScheduleWeek == shift.ScheduleWeek.AddDays(7)
& (s.DayOfWeek == 1 | s.Code == 2))
| (sch.ScheduleWeek == shift.ScheduleWeek.AddDays(-7)
& (s.DayOfWeek == 5 | s.Code == 6)))**
select sch)
.OrderBy(sch => sch.ScheduleWeek)
.ThenBy(sch => sch.DayOfWeek)
.ToList();
First port of call: use && instead of & and || instead of |. Otherwise all the subexpressions in the where clause will be evaluated, even if the answer is already known.
Second port of call: use a join instead of two "from" clauses with a where:
var filteredSchedule = (from sch in _schedule
join s in _shifts on s.ShiftID equals sch.ShiftID
where ... rest of the condition ...
Basically that's going to create a hash of all the shift IDs, so it can quickly look up possible matches for each schedule.
I'm trying to merge these two object but not totally sure how.. Can you help me merge these two result objects?
//
// Create Linq Query for all segments in "CognosSecurity"
//
var userListAuthoritative = (from c in ctx.CognosSecurities
where (c.SecurityType == 1 || c.SecurityType == 2)
select new {c.SecurityType, c.LoginName , c.SecurityName}).Distinct();
//
// Create Linq Query for all segments in "CognosSecurity"
//
var userListAuthoritative3 = (from c in ctx.CognosSecurities
where c.SecurityType == 3 || c.SecurityType == 0
select new {c.SecurityType , c.LoginName }).Distinct();
I think I see where to go with this... but to answer the question the types of the objects are int, string, string for SecurityType, LoginName , and SecurityName respectively
If you're wondering why I have them broken like this is because I want to ignore one column when doing a distinct. Here are the SQL queries that I'm converting to SQL.
select distinct SecurityType, LoginName, 'Segment'+'-'+SecurityName
FROM [NFPDW].[dbo].[CognosSecurity]
where SecurityType =1
select distinct SecurityType, LoginName, 'Business Line'+'-'+SecurityName
FROM [NFPDW].[dbo].[CognosSecurity]
where SecurityType =2
select distinct SecurityType, LoginName, SecurityName
FROM [NFPDW].[dbo].[CognosSecurity]
where SecurityType in (1,2)
You can't join these because the types are different (first has 3 properties in the resulting type, second has two).
If you can tolerate putting a null value in for the 3rd result of the second query this will help. I would then suggest you just do a userListAuthoritative.concat(userListAuthoritative3 ) BUT I think this will not work as the anonymous types generated by the linq will not be of the same class, even tho the structure is the same. To solve that you can either define a CustomType to encapsulate the tuple and do select new CustomType{ ... } in both queries or postprocess the results using select() in a similar fashion.
Acutally the latter select() approach will also allow you to solve the parameter count mismatch by implementing the select with a null in the post-process to CustomType.
EDIT: According to the comment below once the structures are the same the anonymous types will be the same.
I assume that you want to keep the results distinct:
var merged = userListAuthoritative.Concat(userListAuthoritative3).Distinct();
And, as Mike Q pointed out, you need to make sure that your types match, either by giving the anonymous types the same signature, or by creating your own POCO class specifically for this purpose.
Edit
If I understand your edit, you want your Distinct to ignore the SecurityName column. Is that correct?
var userListAuthoritative = from c in ctx.CognosSecurities
where new[]{0,1,2,3}.Contains(c.SecurityType)
group new {c.SecurityType, c.LoginName, c.SecurityName}
by new {c.SecurityType, c.LoginName}
select g.FirstOrDefault();
I'm not exactly sure what you mean by merge, since you're returning different (anonymous) types from each one. Is there a reason the following doesn't work for you?
var userListAuthoritative = (from c in ctx.CognosSecurities
where (c.SecurityType == 1 || c.SecurityType == 2 || c.SecurityType == 3 || c.SecurityType == 0)
select new {c.SecurityType, c.LoginName , c.SecurityName}).Distinct();
Edit: This assumed they were of the same type -- but they're not.
userListAuthoritative.Concat(userListAuthoritative3);
Try below code, you might need to implement IEqualityComparer<T> in your ctx type.
var merged = userListAuthoritative.Union(userListAuthoritative3);
I have a table in SQL database:
ID Data Value
1 1 0.1
1 2 0.4
2 10 0.3
2 11 0.2
3 10 0.5
3 11 0.6
For each unique value in Data, I want to filter out the row with the largest ID. For example: In the table above, I want to filter out the third and fourth row because the fifth and sixth rows have the same Data values but their IDs (3) are larger (2 in the third and fourth row).
I tried this in Linq to Entities:
IQueryable<DerivedRate> test = ObjectContext.DerivedRates.OrderBy(d => d.Data).ThenBy(d => d.ID).SkipWhile((d, index) => (index == size - 1) || (d.ID != ObjectContext.DerivedRates.ElementAt(index + 1).ID));
Basically, I am sorting the list and removing the duplicates by checking if the next element has an identical ID.
However, this doesn't work because SkipWhile(index) and ElementAt(index) aren't supported in Linq to Entities. I don't want to pull the entire gigantic table into an array before sorting it. Is there a way?
You can use the GroupBy and Max function for that.
IQueryable<DerivedRate> test = (from d in ObjectContext.DerivedRates
let grouped = ObjectContext.DerivedRates.GroupBy(dr => dr.Data).First()
where d.Data == grouped.Key && d.ID == grouped.Max(dg => dg.ID)
orderby d.Data
select d);
Femaref's solution is interesting, unfortunately, it doesn't work because an exception is thrown whenever "ObjectContext.DerivedRates.GroupBy(dr => dr.Data).First()" is executed.
His idea has inspired me for another solution, something like this:
var query = from d in ObjectContext.ProviderRates
where d.ValueDate == valueDate && d.RevisionID <= valueDateRevision.RevisionID
group d by d.RateDefID into g
select g.OrderByDescending(dd => dd.RevisionID).FirstOrDefault();
Now this works.