I am trying to pull and read the entity from Dynamics CRM using C#. I am using retrieveMultiple method and all I am getting is Microsoft.Xrm.Sdk.OptionSetValue. When I debug I see 1000 records but every record is showing the same text Microsoft.Xrm.Sdk.OptionSetValue. What am I doing wrong here?
QueryExpression bookQuery = new QueryExpression("new_res")
{
ColumnSet = new ColumnSet("new_book"),
Criteria =
{
Conditions =
{
new ConditionExpression()
{
AttributeName="new_bookid",
Operator = ConditionOperator.NotNull
}
}
}
};
DataCollection<Entity> bookList = service.RetrieveMultiple(bookeQuery).Entities;
foreach (var c in bookList)
{
Console.WriteLine(c.Attributes["new_bookid"]);
}
I just needed to use an EntityCollection:
EntityCollection bookList = (EntityCollection)service.RetrieveMultiple(bookQuery);
if (bookList.Entities.Count > 0)
{
var record = bookList.Entities[0];
var recordNumberString = string.Empty;
}
Related
I have the below hierarchy in my firebase realtime databse, I want to get all children with specific value of the "status" child. How can I do that? Thanks in advance
Firebase Hierarchy
You need to add
First open database ;
FirestoreDbBuilder builder = new FirestoreDbBuilder();
//I am reading configs from json
using (var stream = new StreamReader(#"appsettings.json"))
{
var obj = JObject.Parse(stream.ReadToEnd());
var test = obj.SelectToken("AuthTokenOptions");
builder.JsonCredentials = test.ToString();
builder.ProjectId = "projectId";
firestoreDb = builder.Build();
}
collection = firestoreDb.Collection(_collectionName);
Then you need to Query with your status Column
Query _query = collection.WhereEqualTo("status", "reserved");
QuerySnapshot allQuerySnapshot = await _query .GetSnapshotAsync(token);
List<TEntity> entities = new List<TEntity>();
if (allQuerySnapshot.Count > 0)
foreach (DocumentSnapshot documentSnapshot in allQuerySnapshot.Documents)
{
entities.Add(documentSnapshot.ConvertTo<TEntity>());
};
return entities;
IEnumerable<WebsiteWebPage> data = GetWebPages();
foreach (var value in data)
{
if (value.WebPage.Contains(".htm"))
{
WebsiteWebPage pagesinfo = new WebsiteWebPage();
pagesinfo.WebPage = value.WebPage;
pagesinfo.WebsiteId = websiteid;
db.WebsiteWebPages.Add(pagesinfo);
}
}
db.SaveChanges();
I want to add only distinct values to database in above code. Kindly help me how to do it as I am not able to find any solution.
IEnumerable<WebsiteWebPage> data = GetWebPages();
foreach (var value in data)
{
if (value.WebPage.Contains(".htm"))
{
var a = db.WebsiteWebPages.Where(i => i.WebPage == value.WebPage.ToString()).ToList();
if (a.Count == 0)
{
WebsiteWebPage pagesinfo = new WebsiteWebPage();
pagesinfo.WebPage = value.WebPage;
pagesinfo.WebsiteId = websiteid;
db.WebsiteWebPages.Add(pagesinfo);
db.SaveChanges();
}
}
}
This is the code that I used to add distinct data.I hope it helps
In addition to the code sample Furkan Öztürk supplied, Make sure your DB has a constraint so that you cannot enter duplicate values in the column. Belt and braces approach.
I assume that by "distinct values" you mean "distinct value.WebPage values":
// get existing values (if you ever need this)
var existingWebPages = db.WebsiteWebPages.Select(v => v.WebPage);
// get your pages
var webPages = GetWebPages().Where(v => v.WebPage.Contains(".htm"));
// get distinct WebPage values except existing ones
var distinctWebPages = webPages.Select(v => v.WebPage).Distinct().Except(existingWebPages);
// create WebsiteWebPage objects
var websiteWebPages = distinctWebPages.Select(v =>
new WebsiteWebPage { WebPage = v, WebsiteId = websiteid});
// save all at once
db.WebsiteWebPages.AddRange(websiteWebPages);
db.SaveChanges();
Assuming that you need them to be unique by WebPage and WebSiteId
IEnumerable<WebsiteWebPage> data = GetWebPages();
foreach (var value in data)
{
if (value.WebPage.Contains(".htm"))
{
WebsiteWebPage pagesinfo = new WebsiteWebPage();
if (db.WebsiteWebPages.All(c=>c.WebPage != value.WebPage|| c.WebsiteId != websiteid))
{
pagesinfo.WebPage = value.WebPage;
pagesinfo.WebsiteId = websiteid;
db.WebsiteWebPages.Add(pagesinfo);
}
}
}
db.SaveChanges();
UPDATE
To optimize this (given that your table contains much more data than your current list), override your equals in WebsiteWebPage class to define your uniqueness criteria then:
var myWebsiteWebPages = data.select(x=> new WebsiteWebPage { WebPage = x.WebPage, WebsiteId = websiteid}).Distinct();
var duplicates = db.WebsiteWebPages.Where(x=> myWebsiteWebPage.Contains(x));
db.WebsiteWebPages.AddRange(myWebsiteWebPages.Where(x=> !duplicates.Contains(x)));
this is a one database query to retrieve ONLY duplicates and then removing them from the list
You can use the following code,
IEnumerable<WebsiteWebPage> data = GetWebPages();
var templist = new List<WebsiteWebPage>();
foreach (var value in data)
{
if (value.WebPage.Contains(".htm"))
{
WebsiteWebPage pagesinfo = new WebsiteWebPage();
pagesinfo.WebPage = value.WebPage;
pagesinfo.WebsiteId = websiteid;
templist.Add(pagesinfo);
}
}
var distinctList = templist.GroupBy(x => x.WebsiteId).Select(group => group.First()).ToList();
db.WebsiteWebPages.AddRange(distinctList);
db.SaveChanges();
Or you can use MoreLINQ here to filter distinct the list by parameter like,
var res = tempList.Distinct(x=>x.WebsiteId).ToList();
db.WebsiteWebPages.AddRange(res);
db.SaveChanges();
I am using following code snippet to set the Invoice ID of Invoices in plugin pre-operation. But I am unable to do so. I want to seek your kind suggestion to set the value.
Update
QueryExpression qe = new QueryExpression
{
EntityName = "invoice",
ColumnSet = new ColumnSet("salesorderid", "invoicenumber"),
Criteria = new FilterExpression
{
Conditions = {
new ConditionExpression("salesorderid",ConditionOperator.Equal,orderId)
}
}
};
EntityCollection ec = service.RetrieveMultiple(qe);
if (ec.Entities.Count == 0)
{
string orderName = generateInvoiceID(service, orderId);
foreach (Entity id in ec.Entities)
{
id.Attributes["invoicenumber"] = Convert.ToInt32(orderName) + 01;
}
}
Looking at the snippet looks like you have the plugin registered on "SalesOrder" entity and you are trying to update invoice entity, so it does not matter if it is a pre-op or a post-op, you would need to explicitly call IOrganizationService.Update
var qe = new QueryExpression
{
EntityName = "invoice",
ColumnSet = new ColumnSet("salesorderid", "invoicenumber"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("salesorderid", ConditionOperator.Equal, orderId)
}
}
};
var ec = service.RetrieveMultiple(qe);
var orderName = generateInvoiceID(service, orderId);
foreach (var entity in ec.Entities)
{
var invoice = new Entity("invoice") { Id = entity.Id };
invoice.Attributes.Add("invoicenumber", Convert.ToInt32(orderName) + 01);
service.Update(invoice); //call the update method.
}
I am having trouble building a conditional query using C# MongoDB driver. Whenever I run the code below, I get an empty list. Any help will be greatly appreciated.
Here is my function
public async void searchBook()
{
Book book = new Book();
IMongoDatabase mdb = MongoDBConnectionManager.ConnectToMongoDB();
var query = new BsonDocument();
if (queryString.ContainsKey("Title"))
{
query.Add("Title", queryString["Title"]);
}
if (queryString.ContainsKey("ISBN"))
{
query.Add("Isbn", queryString["ISBN"]);
}
if (queryString.ContainsKey("Author"))
{
query.Add("Author", queryString["Author"]);
}
if (queryString.ContainsKey("Publisher"))
{
query.Add("Publisher", queryString["Publisher"]);
}
var collection = mdb.GetCollection<Book>("Book");
var sort = Builders<Book>.Sort.Ascending("Title");
if(query.ElementCount > 0)
{
var list = await collection.Find(query).Sort(sort).ToListAsync();
dt = ConvertToDataTable(list);
BindGridView(dt);
}
else
{
var list = await collection.Find(Builders<Book>.Filter.Empty).Sort(sort).ToListAsync();
dt = ConvertToDataTable(list);
BindGridView(dt);
}
}
You can use IMongoCollection to get your collection and then use AsQueryable
var query = collection.AsQueryable();
if (!string.IsNullOrEmpty(entity.Name))
query = query.Where(p => p.Name.Contains(entity.Name));
if (!string.IsNullOrEmpty(entity.Description))
query = query.Where(p => p.Description.Contains(entity.Description));
var YourList=query.ToList();
I wrote a LINQ query but I have some problem in executing where clause in foreach loop.
using (DataClasses1DataContext db = new DataClasses1DataContext(("ConnectionString")))
{
Table<NOTIF_SCHED> NOTIF_SCHED_alias = db.GetTable<NOTIF_SCHED>();
IQueryable<NOTIF_SCHED> notif_sched_data = from sched in NOTIF_SCHED_alias select sched;
foreach (var notif_sched_data_value in notif_sched_data)
{
string a = notif_sched_data_value.NOTIF_RPT_ID.ToString();
Table<mainframe_replication> mainframe_replications_alias = db.GetTable<mainframe_replication>();
IQueryable<mainframe_replication> mainframe_replications_data =
from mfrepl in mainframe_replications_alias
where (mfrepl.RPT_ID.Equals(a))
select mfrepl;
foreach (var mainframe_replication_data_value in mainframe_replications_data)
{
Console.WriteLine("hi");
}
}
}
I am not able to use Where clause in line:
IQueryable<mainframe_replication> mainframe_replications_data =
from mfrepl in mainframe_replications_alias
where (mfrepl.RPT_ID.Equals(a))
select mfrepl;**
Can someone please help and check if the syntax is wrong.
This should do the same thing and run much faster:
using (DataClasses1DataContext db = new DataClasses1DataContext(("ConnectionString")))
{
var ids = db.NOTIF_SCHEDs.Select(x=>x.NOTIF_RPT_ID).ToArray();
var repl = db.mainframe_replication
.Where(mfrepl=>ids.Contains(mfrepl.RPT_ID));
foreach (var mainframe_replication_data_value in repl)
{
Console.WriteLine("hi");
}
}
}
If NOTIF_RPT_ID is not a string, and RPT_ID is, you can do this:
using (DataClasses1DataContext db = new DataClasses1DataContext(("ConnectionString")))
{
var ids = db.NOTIF_SCHEDs.Select(x=>x.NOTIF_RPT_ID.ToString()).ToArray();
var repl = db.mainframe_replication
.Where(mfrepl=>ids.Contains(mfrepl.RPT_ID));
foreach (var mainframe_replication_data_value in repl)
{
Console.WriteLine("hi");
}
}
}