[Solved] How can I write this stored procedure with EF in C# [closed]

SO is not a website when you throw everything here and expected people finish the job for you. Anyway, to give you some hint, I give you a straight suggestion: Select CostGroupId From CostGroups Where CostGroupType = 1 –> Stored these in A collection, like an array: var costGroupsIdArr = ctx.CostGroup.Where(x=>x.CostGroupType == 1).Select(x.CostGroupId).toArray(); Then from … Read more

[Solved] Linq: help me to make this work

You should group all person skills by person id, and then select only those groups, which contain both given category and sub category: unitOfWork.PersonSkillsRepository.GetAll() .GroupBy(p => p.PersonId) .Where(g => g.Any(p => p.IdCategory == ids.IdCategory) && g.Any(p => p.IdSubCategory == ids.IdSubCategory)) .Select(g => g.Key) For optimization, you can filter out skills which do not match any … Read more

[Solved] Writing a method which select all the rows and columns of the table given using Entity Framework

Can you write this method on the DbContext class? Then you could write something like in a separate new class file (e.g. ActionContextExtensions.cs or something like that): public partial class ActionContext : DbContext { public IEnumerable<T> SelectAll<T>() where T: class { return this.Set<T>().AsEnumerable(); } } This returns the type as defined by the generic parameter … Read more

[Solved] Sort a Linq query

Start by breaking up your logic into separate operations rather than trying to write everything in one line. Compilers are smart cookies so they optimize things quite well, there’s no point making your code harder to read/understand: var facultySpecialties = specialty.faculty_specialties .Where(fs => fs.faculty.active) .ToList(); // Executes the query to retrieve faculty specialties. foreach(var facultySpecialty … Read more

[Solved] Can someone help me find the bottleneck in this code?

That’s because allLogs is lazy in nature — it will actually hit the database when first enumerated through in the foreach (see “Deferred query execution” here). If you materialize the collection before hand like the following, you will see that it steps through the foreach quickly: db.StatusLogs .Where(sl => sl.ID.Value.Equals(foo)) .GroupBy(sl=>sl.bar) .ToList(); // <– pull … Read more

[Solved] New class object error : Object reference not set to an instance of an object [duplicate]

This is your problem: _cache.TryGetValue(“CountsStats”, out countStats) out will change the object that countStats is pointing to. If it’s found in the cache, then it will be the cached object. Great! If it isn’t, then it will be null. You need to create a new instance in this case. You should change your code to … Read more

[Solved] How to convert linq query to non-query expression?

If you want to convert this to the method syntax version, you can do this step by step. I like to start at the end and work through to the beginning: select to Select defines the source: .Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault()); group is GroupBy: .GroupBy(s => s.SensorUnitId) .Select(g => g.OrderByDescending(s => s.MeasureDate).FirstOrDefault()); from the … Read more

[Solved] Seeking feedback on my design of LINQ expressions [closed]

You’re comparing apples and oranges. Each of the approaches you list has benefits and drawbacks, so it’s impossible to answer which is the “preferred way.” Likewise, many of these examples will return immediately because they use deferred execution, so they’ll technically run faster, but won’t actually make your program run any faster. But if you’re … Read more