LINQ: Distinct values

Are you trying to be distinct by more than one field? If so, just use an anonymous type and the Distinct operator and it should be okay: var query = doc.Elements(“whatever”) .Select(element => new { id = (int) element.Attribute(“id”), category = (int) element.Attribute(“cat”) }) .Distinct(); If you’re trying to get a distinct set of values … Read more

What’s the difference between IQueryable and IEnumerable [duplicate]

IEnumerable<T> represents a forward-only cursor of T. .NET 3.5 added extension methods that included the LINQ standard query operators like Where and First, with any operators that require predicates or anonymous functions taking Func<T>. IQueryable<T> implements the same LINQ standard query operators, but accepts Expression<Func<T>> for predicates and anonymous functions. Expression<T> is a compiled expression … Read more

The specified type member ‘Date’ is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties

DateTime.Date cannot be converted to SQL. Use EntityFunctions.TruncateTime method to get date part. var eventsCustom = eventCustomRepository .FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference) .Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date); UPDATE: As @shankbond mentioned in comments, in Entity Framework 6 EntityFunctions is obsolete, and you should use DbFunctions class, which is shipped with Entity Framework.

Is it possible to Pivot data using LINQ?

Something like this? List<CustData> myList = GetCustData(); var query = myList .GroupBy(c => c.CustId) .Select(g => new { CustId = g.Key, Jan = g.Where(c => c.OrderDate.Month == 1).Sum(c => c.Qty), Feb = g.Where(c => c.OrderDate.Month == 2).Sum(c => c.Qty), March = g.Where(c => c.OrderDate.Month == 3).Sum(c => c.Qty) }); GroupBy in Linq does not work … Read more

LINQ-to-SQL vs stored procedures? [closed]

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

Entity framework linq query Include() multiple children entities

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)