.Net Entity SQL返回相关信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Collections.Specialized;
namespace TestEsql
{
class EsqlOper
{
//返回基本数据
public List<BackLog> GetData1()
{
List<BackLog> list = new List<BackLog>();
using (OMCAUDITEntities db = new OMCAUDITEntities())
{
db.BackLog.MergeOption = MergeOption.NoTracking;
list = db.BackLog.ToList();
}
return list;
}
//返回一个字段
public string GetOneCol()
{
using (OMCAUDITEntities db = new OMCAUDITEntities())
{
return db.BackLog.FirstOrDefault(d => d.UserName == "Admin").UserName;
}
}
//返回两个字段,其中一个为主键
public Dictionary<DateTime, String> GetTwoCol()
{
using (OMCAUDITEntities db = new OMCAUDITEntities())
{
Dictionary<DateTime, String> dic = db.BackLog.ToDictionary(new Func<BackLog, DateTime>(c => c.LogTime), new Func<BackLog, String>(c => c.UserName));
//也可以直接像下面这样写
Dictionary<DateTime, String> dic2 = db.BackLog.ToDictionary(c => c.LogTime, c => c.UserName);
return dic;
}
}
//返回多个字段
public object GetManyCol()
{
using (OMCAUDITEntities db = new OMCAUDITEntities())
{
object query = db.BackLog.Select(c => new { c.LogTime, c.UserName, c.LogType, c.LoginIp }).ToList();
return query;
}
}
public bool DeleteMany()
{
NameValueCollection conditionvalue = new NameValueCollection();
conditionvalue["Name_like"] = "Winson";
OMCAUDITEntities db = new OMCAUDITEntities();
BackLog blog = new BackLog();
db.DeleteObject(conditionvalue);
if(db.SaveChanges()>0)
return true;
else
return false;
}
public int DeleteManyCol()
{
using (OMCAUDITEntities db = new OMCAUDITEntities())
{
Table_1 tab = db.Table_1.FirstOrDefault(d => d.ID == 2);
if (tab == null)
return 0;
db.DeleteObject(tab);
if (db.SaveChanges() > 0)
{
List<Table_1> list = db.Table_1.Where(d => d.ID > 200).ToList();
foreach (Table_1 st in list)
{
db.DeleteObject(st);
}
return db.SaveChanges();
}
else
{
return 0;
}
}
}
}
}