How to convert array items to another type by LINQ

Code in example converts array of items of type EstimatedCategoriesByUser3_Result into array of items of extended type EstimatedCategoriesByUser4_Result having one extra attribute Currency which takes default value.

var v2List = (
   from v1Item in v1Cache.List 
      /*Has type EstimatedCategoriesByUser3_Result*/
   select new EstimatedCategoriesByUser4_Result
   {
      Currency = null,
      EncryptedName = v1Item.EncryptedName,
      Estimation = v1Item.Estimation,
      ID = v1Item.ID,
      Limit = v1Item.Limit,
      NAME = v1Item.NAME,
      Total = v1Item.Total
   }).ToArray();

Structures are original and extended definitions of serializable data.

Code example was taken from my project of expense tracking application

Advertisements

LINQ to Entities does not recognize the method ‘System.DateTime AddMonths(Int32)’ method

Error message

“LINQ to Entities does not recognize the method ‘System.DateTime AddMonths(Int32)’ method, and this method cannot be translated into a store expression”.

Solution

Use intermediate variable like that

 var dateLast = date.AddMonths(-1);

 List<TodayExpense> expenses =
 (from exp in _db.Expenses
 join expCat in _db.ExpensesCategories on exp.ID equals expCat.ExpenseID
 join cat in _db.Categories on expCat.CategoryID equals cat.ID
 where (exp.DataOwner == userId) &&
 (
 ((exp.Monthly == null || !(bool)exp.Monthly) &&
 exp.Date.Day == date.Day && exp.Date.Month == date.Month && exp.Date.Year == date.Year) ||
 ((exp.Monthly != null && (bool)exp.Monthly) &&
 exp.Date.Day == date.Day &&
 (exp.FirstMonth == null || date >= (DateTime)exp.FirstMonth) &&
 (exp.LastMonth == null || dateLast < (DateTime)exp.LastMonth))
 )
 orderby cat.ID, exp.Currency, exp.Cost descending
 select new TodayExpense
 {
 CategoryEncryptedName = cat.EncryptedName,
 CategoryName = cat.Name,
 Cost = exp.Cost,
 Currency = exp.Currency,
 Date = exp.Date,
 ExpenseEncryptedName = exp.EncryptedName,
 ID = exp.ID,
 Name = exp.Name,
 Note = exp.Note,
 Rating = exp.Rating,
 Importance = (ExpenseImportance?)exp.Importance,
 Project = exp.Project,
 CategoryID = cat.ID
 }).ToList();