I'm trying to use LiteDB, I have the following:
var list2 = new List<Values>();
I've been looking at the LiteDB examples which is the following:
// Get customer collection
var col = db.GetCollection<Customer>("customers");
var customer = new Customer { Id = 1, Name = "John Doe" };
// Insert new customer document
col.Insert(customer);
// Update a document inside a collection
customer.Name = "Joana Doe";
col.Update(customer);
// Index document using a document property
col.EnsureIndex(x => x.Name);
Is there any way I can insert a list of type Values in my case it would be: var list2 = new List();
I can't seem to find anywhere that inserts this way.
Thanks,
This worked for me!!
var col = db.GetCollection<Values>("customers");
col.Insert(list2);
Thanks,
Related
I have sub grid called track and location in event entity. Now I want to retrieve the name of the sub grid values and store that values in the text field.In pathway List field I need to add track name as comma separated if it is associate. If it is disassociate I need to remove the particular value form text field.I am new to plugin. I tried query expression but I don't have any common filed for track and event entity to use filter condition. Can u any one suggest the way to achieve this quickly.
I tried below code:
if (context.MessageName.ToLower() == "associate")
{
// Obtain the target entity from the input parameters.
Entity eventEntity = new Entity("leap_event");
var pathwayList ="" ;
QueryExpression query = new QueryExpression("leap_event");
query.ColumnSet = new ColumnSet(true);
LinkEntity linkEntity1 = new LinkEntity("leap_event", "leap_leap_event_leap_location", "leap_eventid", "leap_eventid", JoinOperator.Inner);
LinkEntity linkEntity2 = new LinkEntity("leap_leap_event_leap_location", "leap_location", "leap_locationid", "leap_locationid", JoinOperator.Inner);
linkEntity1.LinkEntities.Add(linkEntity2);
query.LinkEntities.Add(linkEntity1);
linkEntity2.LinkCriteria = new FilterExpression();
linkEntity2.LinkCriteria.AddCondition(new ConditionExpression("", ConditionOperator.Equal, ""));
EntityCollection collRecords = service.RetrieveMultiple(query);
tracingService.Trace("load");
for (int i = 0; i < collRecords.Entities.Count; i++)
{
tracingService.Trace("load1");
var result = collRecords.Entities[i].GetAttributeValue<string>("leap_name");
Console.WriteLine(result);
pathwayList += result + ",";
tracingService.Trace("pathwayName" + pathwayList);
eventEntity["leap_pathwayList"] = pathwayList;
}
}
you will have to use alias for your linked Entity. Take a sample example below.
I have Account Entity--> (N:N) list member Entity--> (N:N) List Entity.
In your case it would be
leap_event Entity--> (N:N) leap_leap_event_leap_location Entity--> (most probably N:N or 1:N) leap_event_leap_location entity.
In addition when you retrieve attribute you will have to use GetAttributeValue<AliasedValue> Take a look at this article it will help you understand
// Instantiate QueryExpression query
var query = new QueryExpression("account");
// Add columns to query.ColumnSet
query.ColumnSet.AddColumns("name", "accountid");
// Add link-entity listmemberentity
var listmemberentity = query.AddLink("listmember", "accountid", "entityid");
listmemberentity.EntityAlias = "listmemberentity";
// Add link-entity listentity
var listentity = listmemberentity.AddLink("list", "listid", "listid");
listentity.EntityAlias = "listentity";
// Add columns to listentity.Columns
listentity.Columns.AddColumns("listname");
It is not necessary to use linkentity.
The QueryExpression must be performed directly on the associated entity, with filter on lookup field (I assume "leap_eventid" field on "leap_location" entity) :
if (context.MessageName.ToLower() == "associate")
{
var query = new QueryExpression("leap_location");
query.ColumnSet.AddColumn("leap_name");
// Lookup filter
query.Criteria.AddCondition(new ConditionExpression("leap_eventid", ConditionOperator.Equal, context.TargetEntity.Id));
// Optional statecode filter
query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));
var collRecords = service.RetrieveMultiple(query);
if (collRecords.Entities.Count > 0)
{
var pathwayList = string.Empty;
foreach (var location in collRecords.Entities)
{
pathwayList += location.GetAttributeValue<string>("leap_name") + ",";
tracingService.Trace($"pathwayName {pathwayList}");
}
}
var eventEntity = new Entity("leap_event", context.TargetEntity.Id)
{
["leap_pathwayList"] = pathwayList
};
service.Update(eventEntity); // Update required for PostOperation Plugin
}
I want to find a role details with specified username in MongoDb with Drivers in C#.
I don't want to use any builders or linq methods.
I tried this to insert a Bson document and it worked.
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
var collec = database.GetCollection<BsonDocument>("user");
var documnt = new BsonDocument{
{"username", txtUsername.Text},
{"password", txtPassword.Password}
};
var check_count = collec.CountDocuments(documnt);
But when I tried this code to find a role with username its not working:
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
var collec = database.GetCollection<BsonDocument>("user");
var documnt = new BsonDocument{
{"username", "admin"},
{"role", 1}
};
var role = collec.Find(documnt);
txtTestRole.Text = role.ToString();
I got this as output:
enter image description here
You have to materialize the query.
As you query for single record, you can use IAsyncCursorSourceExtensions.SingleOrDefault Method.
Or you can refer to IFindFluent (Extension methods section) to select the method that is best suited to you.
Next, from returned BsonDocument to select the specific field for the displayed value.
var user = collec.Find(documnt)
.SingleOrDefault();
if (user == null)
{
// Handle user not exist
return;
}
txtTestRole.Text = user["role"].ToString();
I have list of logical names for an entity for which I need to retreive the displayname.
For eg I have
List<string> _list=new List<string>();
_list.Add("address_City")
_list.Add("first_name")
The display names for them are Address City and First Name
RetrieveEntityRequest req = new RetrieveEntityRequest();
req.RetrieveAsIfPublished = true;
req.LogicalName = "account";
req.EntityFilters = EntityFilters.Attributes;
RetrieveEntityResponse resp = (RetrieveEntityResponse)_orgService.Execute(req);
for (int iCnt = 0; iCnt < resp.EntityMetadata.Attributes.ToList().Count; iCnt++)
{
if(resp.EntityMetadata.Attributes.ToList()[iCnt].DisplayName.LocalizedLabels.Count>0)
{
string displayName = resp.EntityMetadata.Attributes.ToList()[iCnt].DisplayName.LocalizedLabels[0].Label;
string logicalName = resp.EntityMetadata.Attributes.ToList()[iCnt].LogicalName;
}
}
This code retrieves all the record .Is there a way to create some custom query where I can just pass this List<string> and retrieve there display names?
Linq would work great. Just taking your list of logical names and returning a list of display names would look like this:
List<string> _list = new List<string>()
{
"address_City",
"first_name"
};
List<string> _displayList = new List<string>();
if (entityMetaData.Attributes.Any())
{
_displayList.AddRange(from lName in _list
select (entityMetaData.Attributes.FirstOrDefault(n => n.LogicalName.Equals(lName)))
into attribute
where attribute.DisplayName.UserLocalizedLabel != null
select attribute.DisplayName.UserLocalizedLabel.Label);
}
You could use the same logic when returning a dictionary with the logicalName and displayName. Your response from the entity request already has all the metadata, so you don't lose much time at all sifting through that data and getting what you want.
Okay so i have a mongodb that has a collection that is called videos and in videos i have a field called tags. what i want to do is compare a textbox input with the tags on all videos in the collection and return them to a gridview if a tag matches the input from the textbox. When i create a new video the tags field is a string Array so it is possible to store more than one tag. I am trying to do this in c#. Hope you some of you can help thanks!
Code for creating a new video document.
#region Database Connection
var client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("Database");
#endregion
var videos = db.GetCollection<Video>("Videos");
var name = txtVideoName.Text;
var location = txtVideoLocation.Text;
var description = txtVideoDescription.Text;
var user = txtVideoUserName.Text;
string[] lst = txtVideoTags.Text.Split(new char[] { ',' });
var index = videos.Count();
var id = 0;
if (id <= index)
{
id += (int)index;
}
videos.CreateIndex(IndexKeys.Ascending("Tags"), IndexOptions.SetUnique(false));
var newVideo = new Video(id, name, location, description, lst, user);
videos.Insert(newVideo);
Okay so here is how the search method looks like i have just made the syntax a little diffrent from what Grant Winney ansewred.
var videos = db.GetCollection<Video>("Videos");
string[] txtInput = txtSearchTags.Text.Split(new char[] { ',' });
var query = (from x in videos.AsQueryable<Video>()
where x.Tags.ContainsAny(txtInput)
select x);
This finds all videos with tags that contain a tag specified in the TextBox, assuming the MongoDB driver can properly translate it into a valid query.
var videos = db.GetCollection<Video>("Videos")
.AsQueryable()
.Where(v => v.Tags.Split(',')
.ContainsAny(txtVideoTags.Text.Split(',')))
.ToList();
Make sure you've got using MongoDB.Driver.Linq; at the top.
hiya, i have the following code but when i try and create a new IQuerable i get an error that the interface cannot be implemented, if i take away the new i get a not implemented exception, have had to jump back and work on some old ASP classic sites for past month and for the life of me i can not wake my brain up into C# mode.
Could you please have a look at below and give me some clues on where i'm going wrong:
The code is to create a list of priceItems, but instead of a categoryID (int) i am going to be showing the name as string.
public ActionResult ViewPriceItems(int? page)
{
var crm = 0;
page = GetPage(page);
// try and create items2
IQueryable<ViewPriceItemsModel> items2 = new IQueryable<ViewPriceItemsModel>();
// the data to be paged,but unmodified
var olditems = PriceItem.All().OrderBy(x => x.PriceItemID);
foreach (var item in olditems)
{
// set category as the name not the ID for easier reading
items2.Concat(new [] {new ViewPriceItemsModel {ID = item.PriceItemID,
Name = item.PriceItem_Name,
Category = PriceCategory.SingleOrDefault(
x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
Display = item.PriceItems_DisplayMethod}});
}
crm = olditems.Count() / MaxResultsPerPage;
ViewData["numtpages"] = crm;
ViewData["curtpage"] = page + 1;
// return a paged result set
return View(new PagedList<ViewPriceItemsModel>(items2, page ?? 0, MaxResultsPerPage));
}
many thanks
you do not need to create items2. remove the line with comment try and create items2. Use the following code. I have not tested this. But I hope this works.
var items2 = (from item in olditems
select new ViewPriceItemsModel
{
ID = item.PriceItemID,
Name = item.PriceItem_Name,
Category = PriceCategory.SingleOrDefault(
x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
Display = item.PriceItems_DisplayMethod
}).AsQueryable();