Linq with group by having count
Like this: from c in db.Company group c by c.Name into grp where grp.Count() > 1 select grp.Key Or, using the method syntax: Company .GroupBy(c => c.Name) .Where(grp => grp.Count() > 1) .Select(grp => grp.Key);
Like this: from c in db.Company group c by c.Name into grp where grp.Count() > 1 select grp.Key Or, using the method syntax: Company .GroupBy(c => c.Name) .Where(grp => grp.Count() > 1) .Select(grp => grp.Key);
Entity is trying to convert your Paid property to SQL and can’t because it’s not part of the table schema. What you can do is let Entity query the table with no Paid filter and then filter out the not Paid ones. public ActionResult Index() { var debts = storeDB.Orders //.Where(o => o.Paid == false) … Read more
Entity Framework is trying to execute your projection on the SQL side, where there is no equivalent to string.Format. Use AsEnumerable() to force evaluation of that part with Linq to Objects. Based on the previous answer I have given you I would restructure your query like this: int statusReceived = (int)InvoiceStatuses.Received; var areaIds = user.Areas.Select(x=> … Read more
You can’t do that directly in a “neat” way, but you have a few options. First of all, you can explicitly load the child collection after you’ve fetched the stores. See the Applying filters when explicitly loading related entities section. If you don’t want to make extra trips to the database, you will have to … Read more
If you are using plain LINQ-to-objects and don’t want to take a dependency on an external library it is not hard to achieve what you want. The OrderBy() clause accepts a Func<TSource, TKey> that gets a sort key from a source element. You can define the function outside the OrderBy() clause: Func<Item, Object> orderByFunc = … Read more
Use extension methods. Replace NameOfContext with the name of your object context. public static class Extensions{ public static IQueryable<Company> CompleteCompanies(this NameOfContext context){ return context.Companies .Include(“Employee.Employee_Car”) .Include(“Employee.Employee_Country”) ; } public static Company CompanyById(this NameOfContext context, int companyID){ return context.Companies .Include(“Employee.Employee_Car”) .Include(“Employee.Employee_Country”) .FirstOrDefault(c => c.Id == companyID) ; } } Then your code becomes Company company = … Read more
If you do not call ToList() and your final mapping to the DTO type, you can add Where clauses as you go, and build the results at the end: var query = from u in DataContext.Users where u.Division == strUserDiv && u.Age > 18 && u.Height > strHeightinFeet select u; if (useAge) query = query.Where(u … Read more
Slicing a list top5 = array[:5] To slice a list, there’s a simple syntax: array[start:stop:step] You can omit any parameter. These are all valid: array[start:], array[:stop], array[::step] Slicing a generator import itertools top5 = itertools.islice(my_list, 5) # grab the first five elements You can’t slice a generator directly in Python. itertools.islice() will wrap an object … Read more
Some advantages of LINQ over sprocs: Type safety: I think we all understand this. Abstraction: This is especially true with LINQ-to-Entities. This abstraction also allows the framework to add additional improvements that you can easily take advantage of. PLINQ is an example of adding multi-threading support to LINQ. Code changes are minimal to add this … Read more
It is not about closing connection. EF manages connection correctly. My understanding of this problem is that there are multiple data retrieval commands executed on single connection (or single command with multiple selects) while next DataReader is executed before first one has completed the reading. The only way to avoid the exception is to allow … Read more