In the method below db.SubmitChanges is shown as invalid/not recognized by intellisense.
This is my first attempt to update records in a database using LINQ and the method may contain other logic/syntactical errors that I havent yet uncovered as well. What is causing the SubmitChanges to be incorrect?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
public void updateInfo(RefillViewModel _myRefillViewModel) {
try {
decimal patid = _myRefillViewModel.Patient.Patient_ID;
decimal rxid = _myRefillViewModel.Rx.Rx_ID;
CAHODEntities db = new CAHODEntities();
List<Fill> FillList = db.Fills.Where(p => p.Rx.Rx_ID == rxid && p.Rx.Patient_ID == patid && p.Status == "UnFilled").ToList();
foreach (var item in FillList)
{
if (FillList.Count() == 0)
{
item.Status = "Requested";
}
}
db.SubmitChanges();
}
}
Are you using EF? Because in Entity Framework it's SaveChanges() not SubmitChanges().
Related
I am trying to scrape an economic calendar from a specific website. Actually, I tried many times without any success, I don't know where I am wrong. Can you help me, pls?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Network;
namespace Calendar
{
class Program
{
static void Main(string[] args)
{
var url = "https://www.fxstreet.com/economic-calendar";
var webGet = new HtmlWeb();
if (webGet.Load(url) is HtmlDocument document)
{
var nodes = document.DocumentNode.CssSelect("#fxst-calendartable tbody").ToList();
foreach (var node in nodes)
{
// event_time
Debug.Print(node.CssSelect("div div").Single().InnerText);
// event-title
Debug.Print(node.CssSelect("div a").Single().InnerText);
}
}
Console.WriteLine("");
Console.ReadLine()
}
}
}
What error are you getting?
If you want to publish the event names and times from the website, I am assuming you need to read the table.
You can do so using
HtmlNode tablebody = doc.DocumentNode.SelectSingleNode("//table[#class='fxs_c_table']/tbody");
foreach(HtmlNode tr in tablebody.SelectNodes("./tr[#class='fxs_c_row']"))
{
Console.WriteLine("\nTableRow: ");
foreach(HtmlNode td in tr.SelectNodes("./td"))
{
Console.WriteLine(td.SelectSingleNode("./span").InnerText);
}
}
Get hold of the table with the class attribute and then use relevant XPATH to traverse the elements. Please post the error you are getting with your code.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am invoking Where method on a List and is returning elements that don't satisfy my condition.
Here is my call to the Where method:
IEnumerable<MyObject> list = returnList.Where(p => p.MaxDate != null && p.MinDate != null);
I am expecting to have on "list" IEnumerable only the objects that have both MaxDate and MinDate defined (not null)!
And "list" ends with the same results as my returnList, and actually none of the items on "list" as the MaxDate and MinDate defined (different than null), my where condition was supposed to return no elements in that case, am I right?
Thank you very much in advance
EDIT2 (I added the namespaces I am using, maybe there is some bug with this):
Simple example:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
class MyObject
{
public DateTime? MinDate { get; set; }
public DateTime? MaxDate { get; set; }
public string Description{ get; set; }
}
static void Main(string[] args)
{
List<MyObject> lista = new List<MyObject>();
lista.Add(new MyObject { Description = "123" });
lista.Add(new MyObject { Description = "456" });
lista.Add(new MyObject { Description = "678" });
IEnumerable<MyObject> returnn = lista.Where(p => p.MinDate != null && p.MaxDate != null); //this list contains 3 elements and should have 0!! Microsoft bug???? I can't understand this!
}
}
returnList.Where(p => p.MaxDate.HasValue && p.MinDate.HasValue);
Working example:
https://dotnetfiddle.net/qQrjkC
Edit: even the != null should also work, you should do your tests properly before giving downvotes
Jesus, I am feeling so dumb right now, I was looking at the field "source" in the IEnumerable attribute "returnn", instead of checking the actual ResultsView, I made a ToList() and returned no elements!
I am so sorry lol, maybe someone can close this question...
Thank you all for the efforts everyone! The problem was in front of the computer (me) LOL
We have a generic repository done with code-first technique. As the code is in testing we realized that the generic repository is relatively slower than that of directly accessing the DBContext and loading data. In order to simulate the issue for you all, we simplify the generic repository wrote a new code and that code is pasted below..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DemoTest.Bll.Models;
using DemoTest.Bll.DB;
using MasIt.Common.BackEnd;
using System.Data.Entity;
namespace DemoTest.Bll.Handlers
{
public abstract class MyHandlerBase<E, T>
where E : DbSet<T>
where T : class, IBaseObject
{
public MyHandlerBase(E mySet)
{
dbSet = mySet;
}
public E dbSet;
protected IEnumerable<T> GetAllNew(Func<T, bool> predicate)
{
try
{
return dbSet.Where(predicate).AsEnumerable();
}
catch { return null; }
}
}
public class StudentHandler : MyHandlerBase<DbSet<Student>, Student>
{
public StudentHandler()
:base(DemoDBContext.GetContext().Students)
{
//_entitySet = new DemoDBContext().Students;
}
public List<Student> getList(bool option1)
{
if (option1)
{
// Option 1 this is very fast and hit the Student Constructor only once
IEnumerable<Student> response1 = this.dbSet.Where(x => x.StudentId == 10).AsEnumerable();
return response1.ToList();
}
else
{
// Option 2 this is very slow and hit the Student Constructor the student record count of the table
IEnumerable<Student> response2 = this.GetAllNew(x => x.StudentId == 10);
return response2.ToList();
}
}
}
}
Can anybody say why Option 2 is slower.. it is not just slow, it hits the Student class constructor many times, while option 1 hits the constructor only once.. So it seems to us that option 1 load only the matching record, where as option 2 load all the records and match them in memory to filter records..
Generic Repository is a must.. Any fix is highly appreciated...
Got the fix...
Replacing
"Func<T, bool> predicate" with
"Expression<Func<E, Boolean>>" predicate did the trick..
Man .. a horrible nightmare is just over..
Just starting messing with repository/interfaces and the like and I have an error when selecting a single record which I can't work out.
My controller has:
public ViewResult Detail(int ID)
{
var Details = (from x in repo.GetBreakdown(ID) select new BreakdownDetailViewModel { }).SingleOrDefault();
return View(Details);
}
The statement repo.GetBreakdown(ID) is underlined with the following error:
Could not find an implementation of the query pattern for source type ''. 'Select' not found.
My Interface is showing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Entities;
namespace Domain.Abstract
{
public interface IBreakdownRepository
{
tblBreakdown_Log GetBreakdown(int ID);
IQueryable<tblBreakdown_Log> GetAllBreakdowns { get; }
}
}
And the repository itself has:
public tblBreakdown_Log GetBreakdown(int ID)
{
return (from x in db.tblBreakdown_Logs where x.MB_ID == ID select x).SingleOrDefault();
}
Any ideas on what the issue is here?
Thanks,
Chris
Based on the comment by #Evan
Changed my repository access to:
public IEnumerable<tblBreakdown_Log> GetBreakdown(int ID)
{
return (from x in db.tblBreakdown_Logs where x.MB_ID == ID select x);
}
The interface to the repository now has:
IEnumerable<tblBreakdown_Log> GetBreakdown(int ID);
And my controller access is:
public ViewResult Detail(int ID)
{
var Details = (from x in repo.GetBreakdown(ID) select new BreakdownDetailViewModel {Machine_Status=x.tblMachine_Status.Machine_Status, MB_ID=x.MB_ID});
return View(Details);
}
All working as needed now =]
How do you update records in CRM 2011 using OrganizationServiceContext? Can anyone provide a simple example? Thanks!
This is my code:
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Linq;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Client.Services;
using System.Data.Services;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Web.Security;
using System.Data;
using System.Collections.Specialized;
using System.Web.SessionState;
using System;
using System.Web.Profile;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Text;
using System.Web.Caching;
using Telerik.Web.UI;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Data.Entity;
using System.Data.Entity;
public partial class LeadShareEditPanel : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void imgBtnSaveNote_Click(object sender, ImageClickEventArgs e)
{
Uri organizationUri = new Uri("http://server/CRMT/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("user", "password", "domain");
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
// Get the IOrganizationService
//Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
//a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.
using (var service = new OrganizationService(orgProxy))
using (var context = new CrmOrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
contact.JobTitle = "Developer";
context.UpdateObject(contact);
context.SaveChanges();
contact.EMailAddress1 = "bob#contoso.com";
context.UpdateObject(contact);
context.SaveChanges();
}
}
}
This is an example from the 5.05 SDK help file
using (var service = new OrganizationService(connection))
using (var context = new CrmOrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
contact.JobTitle = "Developer";
context.UpdateObject(contact);
context.SaveChanges();
contact.EMailAddress1 = "bob#contoso.com";
context.UpdateObject(contact);
context.SaveChanges();
}
If you haven't already you can download the SDK from here crm 2011 sdk . I would highly reccomend it as it has lots of great samples. The current version is 5.06.
Hope that helps.