使用linq和DataSet更新两个字段

本文关键字:两个 字段 linq DataSet 更新 使用 | 更新日期: 2023-09-27 18:21:19

我需要更新数据库中的两个字段。以下是规则:1) 如果这些字段(DT_GERACAO和BL_RELATORIO)为空,那么我会更新。

好吧,我的问题是我的应用程序是一个控制台应用程序。我没有上下文,所以我没有使用linq或lambda表达式。

请参阅下面我的完整代码。

public static void Emitir()
        {
            //Relatório com DataSource = ORACLE
            dsPlanoMedico.PLANO_MEDICODataTable dtPlanoMedico = new dsPlanoMedico.PLANO_MEDICODataTable();
            dsPlanoMedico.POC_SOLIC_RELATORIODataTable dtSolicRel = new dsPlanoMedico.POC_SOLIC_RELATORIODataTable();
            dsPlanoMedicoTableAdapters.PLANO_MEDICOTableAdapter adapt = new dsPlanoMedicoTableAdapters.PLANO_MEDICOTableAdapter();
            dsPlanoMedicoTableAdapters.POC_SOLIC_RELATORIOTableAdapter solic_adapt = new dsPlanoMedicoTableAdapters.POC_SOLIC_RELATORIOTableAdapter();
            adapt.Fill(dtPlanoMedico);
            solic_adapt.Fill(dtSolicRel);
            //dtPlanoMedico.Where(i => i.IND_REGULAMENTADO == "S");
            var dv = new System.Data.DataView(dtPlanoMedico);
            dv.RowFilter = "IND_REGULAMENTADO LIKE 'N' and TIPO_REGISTRO_ANS LIKE 'D'";
            //dv.RowFilter = "TIPO_REGISTRO_ANS LIKE 'D'";
            ReportDataSource rds = new ReportDataSource("dsDados", dv);
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;
            viewer.LocalReport.ReportPath = "ReportBD.rdlc";
            //viewer.LocalReport.SetParameters(new ReportParameter("Regulamentado", "S"));
            viewer.LocalReport.DataSources.Add(rds);
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            byte[] bytesPDF = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fsPDF = new FileStream("c:''Relatemp''report.pdf", FileMode.Create);
            fsPDF.Write(bytesPDF, 0, bytesPDF.Length);
            fsPDF.Close();
            fsPDF.Dispose();
            byte[] bytesExcel = viewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fsExcel = new FileStream("c:''Relatemp''report.xls", FileMode.Create);
            fsExcel.Write(bytesExcel, 0, bytesExcel.Length);
            fsExcel.Close();
            fsExcel.Dispose();
            byte[] bytesWord = viewer.LocalReport.Render("Word", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fsWord = new FileStream("c:''Relatemp''report.doc", FileMode.Create);
            fsWord.Write(bytesWord, 0, bytesWord.Length);
            fsWord.Close();
            fsWord.Dispose();
        }

我试过这个,但不起作用:

var query = dtSolicRel.AsEnumerable()
            .Where(x => x.DT_GERACAO == null && x.BL_RELATORIO == null).First();
            query.BL_RELATORIO = bytesPDF;
            query.DT_GERACAO = DateTime.Now;
            solic_adapt.Update(query);
            }

我该怎么办?

编辑

我写了这段代码,但我不想更新这些文件:

DataSet ds = new DataSet();
DataTable dtRel = ds.Tables["POC_SOLIC_RELATORIO"];
var query = dtRel.AsEnumerable()
            .Select(rel => new
            {
                dtGeracao = rel.Field<DateTime>("DT_GERACAO"),
                binario   = rel.Field<byte[]>("BL_RELATORIO")
            });

我尝试了其他代码,但仍然有错误。问题就在这一行:

if(query.BL_RELATORIO == null && query.DT_GERACAO == null)

请参阅下面的代码。

var query = (from i in dtSolicRel
                         //where i.DT_GERACAO == null && i.BL_RELATORIO == null
                         select i).ToList().First();
            if(query.BL_RELATORIO == null && query.DT_GERACAO == null)
            {
                query.BL_RELATORIO = bytesPDF;
                query.DT_GERACAO = DateTime.Now;
                solic_adapt.Update(query);
            }

我这样解决

DataSet ds = new DataSet();
            DataTable dt = ds.Tables["POC_SOLIC_RELATORIO"];
            var rowsWithAccount = (from row in dtSolicRel.AsEnumerable()
                                  where row.Field<object>("DT_GERACAO") == null && row.Field<object>("BL_RELATORIO") == null
                                  select row).First();
            for (int i = 0; i < 1; i++)
            {
                rowsWithAccount.SetField("DT_GERACAO", DateTime.Now);
                rowsWithAccount.SetField("BL_RELATORIO", bytesPDF);
                solic_adapt.Update(rowsWithAccount);
            }

我回答了,但回复消失了,然后我决定编辑帖子。

使用linq和DataSet更新两个字段

假设您正确Fill DataTable,则可以执行以下

DataTable dtRel = new DataTable();
var queryRows = (from dRow in dtRel.AsEnumerable()
                    select new 
                    { 
                       dtGeracao = dRow.Field<DateTime>("DT_GERACAO"),
                       binario   = dRow.Field<byte[]>("BL_RELATORIO")
                    });     

我这样做并工作了:

 DataSet ds = new DataSet();
            DataTable dt = ds.Tables["POC_SOLIC_RELATORIO"];
            var rowsWithAccount = (from row in dtSolicRel.AsEnumerable()
                                  where row.Field<object>("DT_GERACAO") == null && row.Field<object>("BL_RELATORIO") == null
                                  select row);
            foreach (DataRow row in rowsWithAccount)
            {
                row.SetField("DT_GERACAO", DateTime.Now);
                row.SetField("BL_RELATORIO", bytesPDF);
                solic_adapt.Update(row);
            }