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.
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 deserialized a JsonResponse using the below code.
var data = (JObject)JsonConvert.DeserializeObject(jsonResponse);
I got the response string which looks something like this
{
"results":[
{
"url":"tickets/2063.json",
"id":20794,
"subject":"Device not working",
"created_date": "2018-01-10T13:03:23Z",
"custom-fields":[
{
"id":25181002,
"value":34534
},
{
"id":2518164,
"value":252344
}
]
}
]
}
My objective is to read certain fields in this array of json objects and insert into a database. The fields i require are id, subject, created_date, member_id.
The member id is part of the custom fields. member_id is the value where id=2518164. I've used List to store this, can you let me know if List or Dictionary is better for this case. How to implement a dictionary
var data = (JObject)JsonConvert.DeserializeObject(jsonResponse);
var tickets = data["results"].ToList();
foreach (var ticketItem in tickets){
Int64? ticketFormId = ticketItem["id"].Value<Int64>();
string subject = ticketItem["subject"].Value<string>();
DateTime createdDate = ticketItem["created_date"].Value<DateTime>();
//Do you think for the next step a dictionary is better or a List is better, since I want to search for a particular id=2518164
var fieldsList = ticketItem["fields"].ToList();
foreach(var fieldItem in fieldList){
Int64? fieldId = fieldItem["id"].Value<Int64>();
if(fieldId!=null && fieldId == 2518164){
memberId = fieldItem["value"].Value<string>();
}
}
}
If you're next step to insert them all into the database, just store them into a list. A dictionary is only useful to search the item by a key.
You can also use linq to process the json in a simpler way:
var tickets = JObject.Parse(jsonResponse)["results"]
.Select(ticket => new
{
Id = (long)ticket["id"],
Subject = (string)ticket["subject"],
CreatedDate = (DateTime)ticket["created_date"],
MemberId = (long)ticket["custom-fields"]
.FirstOrDefault(cf => (int)cf["id"] == 2518164)
?["value"],
})
.ToList();
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,
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.
I have a dictionary:
<string,List<string>>
The key is the product code say "product1" then the list is a list of properties:
"Brand","10.40","64","red","S"
Then I 'can' have a list of rules/filters e.g.
var tmpFilter = new customfilters();
tmpFilter.Field = "2";
tmpFilter.Expression = ">";
tmpFilter.Filter = "10";
So for the above example this would pass because at index 2 (tmpFilter.Field) it is more than 10; then I have another object which defines which fields within the list I want to write to file. For that dictionary item I just want to write the product brand and price where the filters match.
At the moment without the filter I have:
var tmp = new custom();
tmp.Columns = "0,1";
tmp.Delimiter = ",";
tmp.Extention = ".csv";
tmp.CustomFilters = new List<customfilters>() {new customfilters(){ Field = "2", Expression = ">", Filter = "10"} };
public static void Custom(custom custom)
{
foreach (var x in Settings.Prods)
{
//Get Current Product Code
var curprod = Settings.ProductInformation[x];// the dictionary value
foreach (var column in custom.Columns)
{
var curVal = curprod[Convert.ToInt32(column)];
tsw.Write(curVal + custom.Delimiter);
}
Settings.Lines++;
tsw.WriteLine();
}
tsw.Close();
}
I only want to write the curprod if all the filters pass for that list of strings.
How I can do this?
There's a really nice Nuget package based on an example published by Microsoft, that they have decided to make really hard to find for some reason, that allows dynamic linq queries:
https://www.nuget.org/packages/System.Linq.Dynamic/1.0.2
Source:
https://github.com/kahanu/System.Linq.Dynamic
Using that you can do stuff like this very easily (note: I used strings here because the OP states they have a List<string>):
List<string> stuff = new List<string> { "10.40", "64", "5", "56", "99", "2" };
var selected = stuff.Select(s => new { d = double.Parse(s) }).Where("d > 10");
Console.WriteLine(string.Join(", ", selected.Select(s => s.d.ToString()).ToArray()));
Outputs:
10.4, 64, 56, 99
That may give you a place to start. One thing you are going to have to tackle is identifying which of your fields are numeric and should be converted to a numeric type before trying to apply your filter. Otherwise you are going to comparing as strings.