Search This Blog

Friday, July 29, 2016

Get value from object using Lambda expression

C# > Linq > Expressions > Expression > Compile

Compiles the lambda expression described by the expression tree into executable code and produces a delegate that represents the lambda expression.

Example

Get value from object using Lambda expression

using System;
using System.Linq.Expressions;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        public class Project
        {
            public string Name { get; set; }
            public int Id { get; set; }
        }

        public TProp GetValue(TObj obj, Expression<Func> prop)
        {
            return prop.Compile()(obj);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var project = new Project();
            project.Name = "Project 1";
            project.Id = 1;

            int id = GetValue(project, i => i.Id);
            string name = GetValue(project, i => i.Name);

        }
    }

}

SQLITE Get All Tables

SQLITE > Get all tables

SELECT name FROM sqlite_master  WHERE type='table'

Friday, July 8, 2016

SQLite C# Example

C# > SQLite

SQLiteConnection.CreateFile("MyDatabase.sqlite");
SQLiteConnection  m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");

m_dbConnection.Open();

string sql = "create table project (name varchar(100), id int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

sql = "insert into project (name, id) values ('project 1', 1)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

sql = "select * from project order by id desc";
command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())

 Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["id"]);