just wondering I am using a web method to return some json data to a web form. anyway I don't want the entire class in json just two of the columns. so I used linq to get the columns here is an example.
IEnumerable<Person> model = new List<Person>
{
new Person { id = 1, Name = "Bryan", Phone = "218-0211", Email = "bryan#mail.mil" },
new Person { id = 2, Name = "Joe", Phone = "248-0241", Email = "joe#mail.mil" },
new Person { id = 3, Name = "Fred", Phone = "354-0441", Email = "fred#mail.mil" },
new Person { id = 4, Name = "Mary", Phone = "344-3451", Email = "mary#mail.mil" },
new Person { id = 5, Name = "Jill", Phone = "127-3451", Email = "jill#mail.mil" }
};
var mysubset = from a in model select new { a. Name, a.Email };
unfotunately when I then serialize my result and send it back I lose the column names. so data.name doesn't work so I was wondering can I give names to a var type? for example is there a way to do this?
var mysubset = from a in model select new { a. Name, a.Email };
string myname as string;
foreach (var item in mysubset)
{
myname = subset.Name;
}
ok here is the actual code sorry it is in vb but that is the project I inherited
Dim mycollection = From a in cmpnyList select {a.CmpnyName, a.ShipFrom}
return jsSerialize.Serialize(mycollection)
the json returned from that is
[{"Company A","New York"},{"Company B", "Harrisburg"}]
so I'm trying to get back something like
[{"CmpnyName":"Company A", "ShipFrom": "New York"},
{"CmpnyName": "Company B", "ShipFrom": "Harrisburg}]
I believe you're using json.net the answer would be something like this using jobject(newtonsoft.json.linq)
JObject o = new JObject(
new JProperty("PersonssList",
new JArray(
from p in model
select new JObject(
new JProperty("PersonName", p.Name),
new JProperty("Email", p.Email)))));
and the result would be this json
{
"PersonssList": [
{
"PersonName": "Bryan",
"Email": "bryan#mail.mil"
},
{
"PersonName": "Joe",
"Email": "joe#mail.mil"
},
{
"PersonName": "Fred",
"Email": "fred#mail.mil"
},
{
"PersonName": "Mary",
"Email": "mary#mail.mil"
},
{
"PersonName": "Jill",
"Email": "jill#mail.mil"
}
]
Related
I am having a hard time converting this generic template of facebook to C#. I am not sure if i converted it right. Below is the code i tried but is not rendering on messenger. Thank you.
curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{
"id":"<PSID>"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Welcome!",
"image_url":"https://petersfancybrownhats.com/company_image.png",
"subtitle":"We have the right hat for everyone.",
"default_action": {
"type": "web_url",
"url": "https://petersfancybrownhats.com/view?item=103",
"webview_height_ratio": "tall",
},
"buttons":[
{
"type":"web_url",
"url":"https://petersfancybrownhats.com",
"title":"View Website"
},{
"type":"postback",
"title":"Start Chatting",
"payload":"DEVELOPER_DEFINED_PAYLOAD"
}
]
}
]
}
}
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"
This is what i tried in c# but it is not working. I am not sure if i converted it the proper way. Any help would be appreciated thank you.
Activity previewReply = stepContext.Context.Activity.CreateReply();
previewReply.ChannelData = JObject.FromObject(
new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new
{
title = "title",
subtitle = "subtitle",
image_url = "https://thechangreport.com/img/lightning.png",
buttons = new object[]
{
new
{
type = "element_share,",
share_contents = new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new
{
title = "x",
subtitle = "xx",
image_url = "https://thechangreport.com/img/lightning.png",
default_action = new
{
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
},
buttons = new
{
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
title = "Take Quiz",
},
},
},
},
},
},
},
},
},
},
});
await stepContext.Context.SendActivityAsync(previewReply);
The elements and buttons attributes need to be lists. Take a look at the example template below.
var attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new []
{
new {
title = "title",
image_url = "https://thechangreport.com/img/lightning.png",
subtitle = "subtitle",
buttons = new object[]
{
new {
type = "element_share",
share_contents = new {
attachment = new {
type = "template",
payload = new
{
template_type = "generic",
elements = new []
{
new {
title = "title 2",
image_url = "https://thechangreport.com/img/lightning.png",
subtitle = "subtitle 2",
buttons = new object[]
{
new {
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
title = "Take Quiz"
},
},
},
},
},
}
},
},
},
},
},
},
};
reply.ChannelData = JObject.FromObject(new { attachment });
Note, you only need to add a share_contents element to your template if your main template is different from the template you are trying to share. Otherwise, your button can just be new { type = "element_share" }, which makes the template far less complex.
Also, be sure to Whitelist all of your URLs and make sure all of the image URLs work properly - a couple of them weren't working properly. The template won't render if the URLs aren't Whitelisted and image links are broken.
Hope this helps!
I'm pulling contacts from JSON, there are contacts inside Data that might contain emails and phone numbers.
If they do the following query works perfectly:
var results = from d in json.Data
from c in d.Contacts
from e in c.Emails
from p in c.Phones
select new IoModel {Email = e.EmailValue, Id = d.Id, FullName = string.IsNullOrEmpty(c.Name) ? c.Name : d.DisplayName, Phone = p.PhoneNumber, DOB = d.CustomDOB};
If the Data doesn't have Emails or Phones then it runs with no error but it doesn't return the Contact Name.
Is there a way to do this without using if and and's to keep the code shorter and cleaner?
Here is an example of the JSON with a missing email:
{
"data": [
{
"addresses": [],
"contacts": [
{
"created_by": "user_ltUL3eXGPRWb5ghDeGTfOe9qjW0LeE2e4ouopLcSSWj",
"date_created": "2017-12-28T17:13:00.392000+00:00",
"date_updated": "2017-12-28T17:13:58.453000+00:00",
"emails": [],
"id": "cont_6hxxhz51ctlTnHfo8gA8cce0rthS1dJy1kguKAj148s",
"integration_links": [
{
"name": "LinkedIn Search",
"url": "https://www.linkedin.com/search/results/index/?keywords=John%20Doe"
}
],
"lead_id": "lead_3UzfxCgQHmw4BlGwElitHhlP6E7q9Tg3sdSkTl1CIXp",
"name": "John Doe",
"organization_id": "orga_PvgGx1opSZDsCHl73P8OoSFZUlJ3qsNGI2kwgoObM17",
"phones": [
{
"phone": "+15555555555",
"phone_formatted": "+1 555-555-5555",
"type": "mobile"
}
],
"title": "Main Service",
"updated_by": "user_ltUL3eXXPRWb5shDeGTfOe9qjP0LeE2e4ouopLcSSWj",
"urls": []
}
],
"created_by": "user_ltUL3eXXPRWb5shDeGTfOe9qjW0LeE2e4ouopLcSSWP",
"created_by_name": "John",
"custom": {
"Date Of Birth": "1901-08-01",
"Department": "Main Service",
"Initial Service": "Main Service"
},
"custom.lcf_ufMH5ZhlR99zcdvJxKMxdgxcIbV4wtgTb3EdWDEkL8g": "1971-08-17",
"date_created": "2017-12-28T17:13:00.388000+00:00",
"date_updated": "2017-12-29T21:41:53.840000+00:00",
"description": "",
"display_name": "John Doe",
"html_url": "https://app.close.io/lead/lead_3UzfxCgQHmw4BlGwElitHhlP6E7q9Tg3sdSkTl1CIXp/",
"id": "lead_3UzfxCgQHmw4BlGwElitHhlP5E7q9Tg3sdSkTl1CIXp",
"integration_links": [
{
"name": "Google Search",
"url": "http://google.com/search?q=John%20Doe"
}
],
"name": "",
"opportunities": [],
"organization_id": "orga_AvcGx4opSZDsCHl73H8OogDDUlJ3qsNGI2kwgoObA17",
"status_id": "stat_LpJu8FO72WgIob4qDDVnS4GEieoU41zmQ8xBquTvusm",
"status_label": "Established Patient",
"tasks": [],
"updated_by": "user_0JFRnl8QRvRhMhAlLz4JJxgmrzPeLs3xboxYyj5Pm80",
"updated_by_name": "BT",
"url": null
}
],
"has_more": false,
"total_results": 1
}
Since I don't know what you are deserializing your JSON into, I can only guess at a solution here, but I think if you use DefaultIfEmpty() on your Emails and Phones collections and then use the null-conditional operator on the EmailValue and PhoneNumber, you should get the result you want:
var results = from d in json.Data
from c in d.Contacts
from e in c.Emails.DefaultIfEmpty()
from p in c.Phones.DefaultIfEmpty()
select new IoModel
{
Email = e?.EmailValue,
Id = d.Id,
FullName = string.IsNullOrEmpty(c.Name) ? c.Name : d.DisplayName,
Phone = p?.PhoneNumber,
DOB = d.CustomDOB
};
Let me explain the issue you are running into. Imagine the following two lists:
Issue
var ids = new List<int> { 1, 2, 3, 4 };
var names = new List<string>();
Let's write a Linq query to get all the ids along with names:
var idsAndNames = from id in ids
from name in names
select new { Id = id, Name = name };
The above query will return no results. Why? This is more obvious if you were to use a foreach instead of Linq. The foreach version will look roughly like below:
foreach (var id in ids)
{
foreach (var name in names)
{
//select new { Id = id, Name = name };
}
}
It is very clear and obvious why no results will be returned in the foreach version above (the code within the inner loop will not be executed because the names list is empty).
Fix
We need to tell our query: If there are no items in names collection, we will accept the default. We can do that using DefaultIfEmpty method.
var idsAndNames = from id in ids
from name in names.DefaultIfEmpty()
select new { Id = id, Name = name };
The above will return 4 results. It is sort of like doing the following:
foreach (var id in ids)
{
if (names.Any())
{
//select new { Id = id, Name = name };
}
else
{
//select new { Id = id, Name = "" }; // <-- Notice empty string
}
}
Answer to your question
#BrianRogers has answered your question but I elaborated on what the issue is.
I'm trying to compare two C# objects by converting them to JSON and then making a diff. It works fine for all primitives but I don't understand why when I change items in a list that had items the comparison is retuning an object instead of an array.
I'm going to list 3 cases, the first 2 go by as expected:
Case 1 - previous list was empty and one item was added later
var company = new Company(15)
{
Name = "Sega"
};
var previousCat = new Category(1)
{
Name = "Cat X",
Department = new Department(100)
{
Name = "Department 100",
Company = company
},
Companies = new List<Company>()
};
var currentCat = new Category(1)
{
Name = "Cat XYZ",
Department = new Department(100)
{
Name = "Department 100",
Company = company
},
Companies = new List<Company> { company }
};
Result as expected*
// New Value:
[
{
"Name": "Sega",
"Categories": null,
"Id": 15
}
]
// Old value:
[]
Case 2 - previous list had one item that was later removed
var company = new Company(15)
{
Name = "Sega"
};
var previousCat = new Category(1)
{
Name = "Cat X",
Department = new Department(100)
{
Name = "Department 100",
Company = company
},
Companies = new List<Company>
{
company
}
};
var currentCat = new Category(1)
{
Name = "Cat XYZ",
Department = new Department(100)
{
Name = "Department 100",
Company = company
},
Companies = new List<Company>()
};
Result as expected*
// New value:
[]
// Old Value:
[
{
"Name": "Sega",
"Categories": null,
"Id": 15
}
]
Case 3 - both lists have items
var sega = new Company(15)
{
Name = "Sega"
};
var sony = new Company(30)
{
Name = "Sony"
};
var nintendo = new Company(45)
{
Name = "Nintendo"
};
var microsoft = new Company(60)
{
Name = "Microsoft"
};
var previousCat = new Category(1)
{
Name = "Cat X",
Department = new Department(100)
{
Name = "Department 100",
Company = sega
},
Companies = new List<Company>
{
sega,
nintendo
}
};
var currentCat = new Category(1)
{
Name = "Cat XYZ",
Department = new Department(100)
{
Name = "Department 100",
Company = sega
},
Companies = new List<Company>
{
nintendo,
sony,
microsoft
}
};
Result NOT as expected*
// New value:
{
"0": {
"Name": "Nintendo",
"Id": 45
},
"1": {
"Name": "Sony",
"Id": 30
},
"2": {
"Name": "Microsoft",
"Categories": null,
"Id": 60
},
"## Count": 3
}
// Old value:
{
"0": {
"Name": "Sega",
"Id": 15
},
"1": {
"Name": "Nintendo",
"Id": 45
},
"## Count": 2
}
As you can see the third result returns an object, not an array. Why is that?
I'm using this class:
https://github.com/khalidsalomao/SimpleHelpers.Net/blob/master/SimpleHelpers/ObjectDiffPatch.cs
This way:
var diff = ObjectDiffPatch.GenerateDiff(Previous,Current);
That's probably because of the way the code you linked (ObjectDiffPatch) builds up the array diff object. This is the relevant part, as I recreated it in LINQPad:
I added this as an image because that tooltip is important here. But let's start at the top:
If one of the arrays is empty, it properly keeps the original structure: grabs the two arrays (one of which is empty), and adds them under the field name "Companies".
If, however, neither are empty, then it starts comparing their contents, piece by piece. And when a difference is found, the code adds it to the diff object using AddNewValuesToken, which expects a field name as its third parameter, but here we don't have a field name, just an array index.
Now, the code "solves" this by converting the array index to string, and treating it as a field name. No wonder that the resulting JObject will treat it that way too – this is why you see an object in the output, where what used to be array indices, become fields.
How can I Update Customer using Id, SyncToken for QBO IPP .NET SDK V3?
Getting an errror (You and admin#gmail.com were working on this at the same time. ajoshi#varstreet.com finished before you did, so your work was not saved.)
Please suggest me where i am going wrong.
IdType CustID = new IdType();
CustID.Value = "22";
var NewqboCustomer = new Customer()
{
Id = CustID,
SyncToken = "0",
// Display name as Customer Name
Name = "Name",
ShowAs = "Name", //Print on check
//
//Contact Info
Title = "",
GivenName = "Contact1",
MiddleName = "",
FamilyName = "Contact1",
//
//Notes ="Notes ",
Notes = new Note[]
{
new Note()
{
Content ="Test Notes",
}
},
TypeOf = partyType.Organization,
Address = new PhysicalAddress[]
{
new PhysicalAddress()
{
Line1 = "bill Line 1",
Line2 = "Bill Line 2",
Line3 = "Bill Line 3",
Line4 = "Bill Line 4",
Line5 = "Bill Line 5",
City = " Bill City ",
Country ="Bill Country",
CountyCode ="Bill Counrty code",
CountrySubDivisionCode = "NC",
PostalCode = "55712",
PostalCodeSuffix="Postal Code Suffix",
Tag = new string[]{"Billing"}
},
new PhysicalAddress()
{
Line1 = "Ship Line 11",
Line2 = "Ship Line 21",
Line3 = "Ship Line 3",
Line4 = "Ship Line 4",
Line5 = "Ship Line 5",
City = "Ship City ",
Country ="Ship Country",
CountyCode ="Ship Counrty code",
CountrySubDivisionCode = "NC",
PostalCode = "55712",
PostalCodeSuffix="Shippf",
Tag = new string[]{"Shipping"}
}
},
Phone = new TelephoneNumber[]
{
new TelephoneNumber()
{
DeviceType = "Work",
FreeFormNumber = "111-345-3456"
},
new TelephoneNumber()
{
DeviceType = "Mobile",
FreeFormNumber = "111-345-3457"
},
new TelephoneNumber()
{
DeviceType = "Fax",
FreeFormNumber = "111-345-3457"
}
},
WebSite = new WebSiteAddress[]
{
new WebSiteAddress()
{
URI = "http://www.varstreet.com"
}
},
Email = new EmailAddress[]
{
new EmailAddress()
{
Address = "india#varstreet.com"
}
},
SalesTermId = new IdType()
{
//idDomain = idDomainEnum.QBO,
//Value = "8"
},
};
var output = commonService.Update(NewqboCustomer);
Before making any update on any object, you should query that object using findById call to get the latest syncToken. Please use the latest sync token in the next update call.
It will resolve this issue.
GetById Ref - https://developer.intuit.com/docs/0025_quickbooksapi/0055_devkits/0150_ipp_.net_devkit_3.0/0002_synchronous_calls/0001_data_service_apis#FindById()_Method
Similar issue Ref - Quick Book Error You and sam working on this at the same time
Thanks
Unless you do a read just before you update, the metadata is stale/not the latest.
So, if you try to update on this, then you get the error for the conflicting metadata related to the object.
Once, you do the read and your object has the latest metadata, you can then update the object successfully.
The syncToken may/maynot change but metadata does. So, it needs to be refreshed before any update along with the sync token.
<MetaData>
<CreateTime>2013-04-23T18:10:52-07:00</CreateTime>
<LastUpdatedTime>2013-04-23T18:10:52-07:00</LastUpdatedTime>
</MetaData>
Is is possible to concatenate multiple rows to a single row?
for example:
IEnumerable<sample> sam = new List<sample>()
{
new sample{ id = 1, name = "sample 1", list = new List<int>{1,5,6}},
new sample{ id = 2, name = "sample 2", list = new List<int>{2,9}},
new sample{ id = 3, name = "sample 3", list = new List<int>{8,3,7}},
new sample{ id = 4, name = "sample 4", list = new List<int>{3,4,8}},
new sample{ id = 5, name = "sample 5", list = new List<int>{1,5,7}},
new sample{ id = 6, name = "sample 6", list = new List<int>{6,9,7}}
};
output must:
{
new sample { id = 1, name = "sample 1", list = "1,5,6" },
new sample { id = 2, name = "sample 2", list = "2,9" },
new sample { id = 3, name = "sample 3", list = "8,3,7" },
new sample { id = 4, name = "sample 4", list = "3,4,8" },
new sample { id = 5, name = "sample 5", list = "1,5,7" },
new sample { id = 6, name = "sample 6", list = "6,9,7" }
};
That means from list the new row is now a string.
Sure:
sam.Select(x => new { x.id, x.name, list = String.Join(",", x.list) });
Note: The result will be an anonymous type. We can't reuse the sample class here, because in that class list is of type List<int> and not string.
If you are stuck with .NET 3.5 or less, use this code instead:
sam.Select(x => new
{
x.id, x.name,
list = String.Join(",", x.list.Select(y => y.ToString())
.ToArray())
});
If you want to sort the list before getting the string you need to use this code:
sam.Select(x => new
{
x.id, x.name,
list = String.Join(",", x.list.OrderBy(y => y))
});
As I needed to gather several records in one column (each employee may have many specialties) and then use it in a join, this is the way I resolved:
Having these entities:
1) EMPLOYEEs Entity
|EMPLOYEE_ID|
|001 |
|002 |
2) EMPLOYEE_SPECIALTIES Entity
|EmployeeId|SPECIALTY_CODE
|001 |AAA
|001 |BBB
|002 |DDD
|002 |AAA
I needed to gather specialties by employee in one columun:
|EmployeeId|SPECIALTY_CODE
|001 |AAA, BBB
|002 |DDD, AAA
Solution:
var query = from a in context.EMPLOYEE_SPECIALTIES.ToList()
group a by a.EMPLOYEE_ID into g
select new
{
EmployeeId = g.Key,
SpecialtyCode = string.Join(",", g.Select(x =>
x.SPECIALTY_CODE))
};
var query2 = (from a in query
join b in context.EMPLOYEEs on a.EmployeeId equals b.EMPLOYEE_ID
select new EmployeeSpecialtyArea
{
EmployeeId = b.EMPLOYEE_ID,
LastName = b.LAST_NAME,
SpecialtyCode = a.SpecialtyCode
});
ViewBag.EmployeeSpecialtyArea = query2;
I hope this may help someone!