Tuesday, July 19, 2016

Export binary data from database using LINQPAD

Switch LinqPad to c# Program

void Main()
{
    var students = Students.Where(st => st.Photo != null).Select(st => new
    {
        st.Id,
        st.Photo
    });
    foreach (var student in students)
    {
        File.WriteAllBytes(@"D:\Photos\" + student.Id + ".jpg", student.Photo.ToArray());
    }
}

LINQ to Entities does not recognize the method 'System.String PadLeft(Int32)'

Convert to list then do padleft

The entity type List`1 is not part of the model for the current context

where db is List

db.Entry(bd).State = EntityState.Modified;

DbContext.Entry(object) expects a single object, not a collection.

so use a loop instead

foreach (var item in bd)
{
    db.Entry(item).State = EntityState.Modified;
}

Thursday, July 14, 2016

Convert dictionary to list collection in C#

using linq
accountTypes.Select(kvp => new
            {
                account_type_no = kvp.Key,
                account_type_name = kvp.Value
            }).ToList()
using loop
foreach (var item in accountTypes)
{
    accountTypeList.Add(item.Key);
}