EF实体框架模型使用Linq获取数据库数据

来源:互联网 发布:dm500账号上传软件 编辑:程序博客网 时间:2024/05/19 18:48

EF实体框架模型对于处理数据库提供了很大的方便。

EF支持多种数据库,如SQL/ORCAL/DB2等;能够与asp.net/wpf/wcf等很好的集成。

此外最方便的就是可以用Linq语句实现数据库操作。


1、首先生成EF实体框架模型,具体步骤参见链接:http://jingyan.baidu.com/article/359911f5703b1757ff03064f.html

2、然后开始用Linq查询获取数据库数据,代码如下:

public List<ProductionEfficiency> stationProductEfficiencies;
public IQueryable<IGrouping<string, ProductionEfficiency>>stationDailyGroup;

public void GetFiltedProdEfficiency(string factory, string line, string shift, string startDay, string endDay)
        {
           
var stationtemp = from pe in ProductionEfficiencies  //ProductionEfficiencies是数据表名
                              where  //查询条件
                                    pe.SiteCode.Length > 5 &&
                                    (factory == SELECT_ALL || pe.SiteCode.IndexOf(factory) >= 0) &&
                                    (line == SELECT_ALL || pe.SiteCode.IndexOf(line) >= 0) &&
                                    (shift == SELECT_ALL || pe.ShiftCode.IndexOf(shift) >= 0) &&
                                    string.Compare(pe.ShiftCode.Substring(0, 8), startDay) >= 0 &&
                                    string.Compare(pe.ShiftCode.Substring(0, 8), endDay) <= 0 &&
                                    pe.LotNo.Trim().ToUpper() != testLotNo.Trim().ToUpper()
                                    orderby pe.ShiftCode
                              select pe;
            stationProductEfficiencies = stationtemp.ToList();
            stationDailyGroup = from pe in stationtemp group pe by pe.ShiftCode.Substring(0, 8);

       }

其中, IQueryable类型是是专门用于数据库查询分组后的数据保存。(注意,当你自己的处理函数用到了对多个数据表的 IQueryable数据的操作,会报错:数据库已经打开了一个表,等待当前表连接关闭。)取数据的时候没问题,上面那个函数里面,后面还可以再接多个查询别的表的分组操作。


数据库查询中还会存在数据表联合查询的情况,联合查询的Linq语句如下:

public Dictionary<string, List<RejectData>> stationRejectGroup;

var rejectTemp = from pr in ProductionRejects    //数据表ProductionRejects 和ProductionEfficiencies是相互关联的
  
                           join pe in ProductionEfficiencieson pr.EfficiencyID equals pe.EfficiencyID
                             where
                               pe.SiteCode.Length > 5 &&
                               (factory == SELECT_ALL || pe.SiteCode.IndexOf(factory) >= 0) &&
                               (line == SELECT_ALL || pe.SiteCode.IndexOf(line) >= 0) &&
                               (shift == SELECT_ALL || pe.ShiftCode.IndexOf(shift) >= 0) &&
                               string.Compare(pe.ShiftCode.Substring(0, 8), startDay) >= 0 &&
                               string.Compare(pe.ShiftCode.Substring(0, 8), endDay) <= 0 &&
                               pe.LotNo.Trim().ToUpper() != testLotNo.Trim().ToUpper()
                             select new { EffID = pr.EfficiencyID, Station = pe.SiteCode.Substring(2), ShiftCode = pe.ShiftCode, LineCode=pe.SiteCode,FailureMode = pr.FailureMode, Reject = pr.Reject };   //联合查询后抽取自己感兴趣的字段,我是定义了struct RejectData用于保存我需要的字段
         var stationRejectGrouptemp = from re in rejectTemp group re by re.Station;  //stationRejectGrouptemp取出来的分组后的数据
  
       stationRejectGroup = new Dictionary<string, List<RejectData>>();   //另存取到的数据,便于后面做任何操作


//下面是另存数据的动作

foreach (var group in stationRejectGrouptemp)
            {

            List<RejectData> temp = new List<RejectData>();
                    foreach (var rr in group)
                        temp.Add(new RejectData(rr.EffID, rr.Station, rr.ShiftCode, rr.LineCode, rr.FailureMode, rr.Reject));
                    stationRejectGroup.Add(group.Key, temp); 

           }


最后说明一下,在Web.config配置文件里要正确配置数据库的连接字符串,就可以取到数据了。


1 0