说明:下面实例都是我进行项目开发时的真实部分代码,毫无保留
一、数据表的关联计算
//把当前年度的分差计算出来,建立两个关联的数据表
try
{
using(TransactionScope scope=new TransactionScope())
{
zy_admin dq_gly = Session["stjBEF985E"] as zy_admin;//只能显示当前管理员的设置
int nd = DateTime.Now.Year;
var db_qn = from aa in sjklj.Db_fszh
where aa.dwid == dq_gly.dwid
&& aa.nd == nd - 1
select aa;//去年
var db_jn = from aa in sjklj.Db_fszh
where aa.dwid == dq_gly.dwid
&& aa.nd == nd
select aa;//今年
//转换成dataset ,list
/*
List<Db_fszh> l_qn = db_qn.ToList<Db_fszh>();
List<Db_fszh> l_jn = db_jn.ToList<Db_fszh>();
//同表联合查询
var cx_jn = from aa in l_jn
join bb in l_qn on aa.pc equals bb.pc
select aa;
*/
//关联的,运算是批量计算,例如只关联pc,分差为去年文理科同批次之和减去今年文理科同批次之和
var cx_jn = from aa in db_jn
join bb in db_qn
on aa.pc+aa.lb equals bb.pc+bb.lb
select new
{
ID = aa.ID,
pc = aa.pc,
fc = bb.fs - aa.fs
};
//更新今年的fc字段
foreach (var kk in cx_jn)
{
sjklj.Db_fszh.Where(aa => aa.ID == kk.ID).First().fc = kk.fc;
}
sjklj.SaveChanges();
scope.Complete();
layeriujq.Msg("分差修改成功!", "1", "3000", this.Page);
}
}
catch
{
layeriujq.Msg("分差修改成功!","2", "3000", this.Page);
}
二、关于null值得处理
先看看网上查询到的解决方案
linq查询某个字段为null的数据
如tb_flag 数据结构如下
flag int null
如何用linq查询flag为null的数据
flag==null 生成的sql 语句为 where flag=null sql里面貌似 flag is null才能查询出null的数据。 求解答。
------解决方案--------------------
可空类型 用 Nullable<T>.Equals(字段,值)
var query=from f in db.tb_flag
where Nullable<int>.Equals(f.flag,null) select f;
------解决方案--------------------
flag==null
=>
flag==DBNull.Value
------解决方案--------------------
var query=from f in db.tb_flag
where flag==DBNull.Value select f;
------解决方案--------------------
探讨
可空类型 用 Nullable<T>.Equals(字段,值)
var query=from f in db.tb_flag
where Nullable<int>.Equals(f.flag,null) select f;
下面具体在项目中的应用
try
{
zjh = int.Parse(this.tbx_zjh.Text.Trim());
}
catch
{
zjh = null;
}
try
{
lqrs = int.Parse(this.tbx_lqrs.Text.Trim());
}
catch
{
lqrs = null;
}
ispk = this.chk_pk.Checked;
try
{
zdf =double.Parse(this.tbx_zdf.Text.Trim());
}
catch
{
zdf = null;
}
try
{
zgf =double.Parse(this.tbx_zgf.Text.Trim());
}
catch
{
zgf = null;
}
try
{
pjf =double.Parse(this.tbx_pjf.Text.Trim());
}
catch
{
pjf = null;
}
try
{
lqfs =double.Parse(this.tbx_lqfs.Text.Trim());
}
catch
{
lqfs = null;
}
//验证输入有效性完毕,开始判断是否重复加入
try
{
zy_admin dq_gly = Session["stjBEF985E"] as zy_admin;
/*
var cx_xxzy = from aa in sjklj.zy_school
where aa.dwid == dq_gly.dwid
&& aa.dm == dh && aa.xxmc == xxmc && aa.lb == lb && aa.pc == pc
&& aa.pjf == pjf && aa.lqfs == lqfs && aa.zgf == zgf && aa.zdf == zdf
&& aa.zymc == zymc && aa.ispk==ispk
select aa;
*/
/*
var cx_xxzy = from aa in sjklj.zy_school
where aa.dwid == dq_gly.dwid
&& aa.dm =="2153" && aa.xxmc =="辽宁工程技术大学" && aa.lb =="理科" && aa.pc =="贫困专项本科"
&& aa.pjf ==533 && aa.lqfs ==525
&& aa.ispk ==true
select aa;
*/
var cx_xxzy = from aa in sjklj.zy_school
where aa.dwid == dq_gly.dwid
&& aa.dm == dh && aa.xxmc == xxmc && aa.lb == lb && aa.pc == pc
&& (aa.pjf == pjf || Nullable<double>.Equals(aa.pjf, null)) &&
(aa.lqfs == lqfs || Nullable<double>.Equals(aa.lqfs, null))
&& (aa.zgf == zgf || Nullable<double>.Equals(aa.zgf, null)) &&
(aa.zdf == zdf || Nullable<double>.Equals(aa.zdf, null))
&& aa.zymc == zymc && aa.ispk == ispk
select aa;
//无法判断重复?原因查明,对于null,==null 的无法判断,必须用nullable进行对象比较
if(cx_xxzy.Count()>0)
{
layeriujq.Msg("该学校或专业已经添加", "2", "3000", this.Page);
return;
}