I have a simple way to put 2 known profiles in my profileArray list as shown below:
Parameters params = new Parameters();
params.plist = new Plist();
params.plist.profileArray = new[]
{
new Profile{name = "john", age = 12, country = "USA"},
new Profile{name = "Brad", age = 20, country = "USA"}
};
Now i have a
List<Profiles> UserProfiles
which has a bunch of profiles in it.
How do i add this list to params.plist.profileArray?
Any help is appreciated.
Thanks.
This is what is in UserProfile:
List<Profiles> UserProfiles
foreach(Profiles userProfile in UserProfiles)
{
string name = userProfile.Name;
string age = userProfile.Age;
string country = userProfile.Country;
string sex = userProfile.Sex;
string isMarried = userProfile.IsMarried;
}
You can use Enumerable.ToArray:
params.plist.profileArray = UserProfiles.ToArray();
If you want to add the list to the array, an array cannot be modified, you have to create a new one, for example by using Enumerable.Concat:
var newProfile = params.plist.profileArray.Concat(UserProfiles);
params.plist.profileArray = newProfile.ToArray();
Since these are two different classes with similar properties:
var profiles = UserProfiles
.Select(up => new Profile{name = up.Name, age = up.Age, country = up.Country});
var newProfile = params.plist.profileArray.Concat(profiles);
params.plist.profileArray = newProfile.ToArray();
Try this:
params.plist.profileArray = UserProfiles.ToArray();
How about this?
params.plist.profileArray =
UserProfiles
.Select(up => new
{
name = up.Name,
age = up.Age,
country = up.Country,
})
.ToArray();
Try
params.plist.profileArray = params
.plist
.profileArray
.Concat( UserProfiles )
.ToArray()
;
Since you're using an array (and arrays in C# are of fixed size), you'l need to create a new array with the combined data.
There are several ways to do this, the simplest would probably be something like this:
var newList = new List<Profile>();
newList.AddRange(params.plist.profileArray);
newList.AddRange(UserProfiles);
params.plist.profileArray = newList.ToArray();
If can you change the implementation of Plist, I would recommend changing the array to a List<Profile>. Then the code would look like this instead:
params.plist.profileArray.AddRange(UserProfiles);
Related
I have a list with this structure:
public class Amendment{
public string name
public string groupnumber
public string edition
public string destination
}
The data is as follows
var amendmentOne = new Amendment{
name = "Apple",
groupnumber = "A12345",
edition = "A55600E01"
phonenumber = "2232132123"
}
var amendmentTwo = new Amendment{
name = "Apple",
groupnumber = "AG2222",
edition = "A55600E02"
phonenumber = "2232132123"
}
var amendmentThree = new Amendment{
name = "Apple",
groupnumber = "AG55555",
edition = "A55600E03"
phonenumber = "2232132123"
}
Is it possible to somehow get the list item with edition number A55600E03 from the list when I loop through it? The edition numbers are all the same except for the last few characters as they will always be E[number]
Should be as easy as
var result = List.Orderby(x => edition).Last();
Example
var list = new List<Amendment>{amendmentThree, amendmentOne, amendmentTwo};
var result = list.OrderBy(x => x.edition).Last();
Console.WriteLine(result.edition);
Output
A55600E03
Full Demo Here
I am trying out the Amazon MWS samples. How do I initialise request.ASINList with a list of ASINs?
My ASINs are in strings.
// Create a request.
GetLowestOfferListingsForASINRequest request = new GetLowestOfferListingsForASINRequest();
string sellerId = "example";
request.SellerId = sellerId;
string mwsAuthToken = "example";
request.MWSAuthToken = mwsAuthToken;
string marketplaceId = "example";
request.MarketplaceId = marketplaceId;
ASINListType asinList = new ASINListType();
request.ASINList = asinList;
string itemCondition = "example";
request.ItemCondition = itemCondition;
bool excludeMe = true;
request.ExcludeMe = excludeMe;
return this.client.GetLowestOfferListingsForASIN(request);
I can't seem to implicitly or explicitly cast a list or array of strings to ASINListType.
Don't know c# but in PHP you have to create an object of class "MarketplaceWebServiceProducts_Model_ASINListType"
e.g.
$asin_list = new MarketplaceWebServiceProducts_Model_ASINListType();
$asin_list->setASIN($asin_array);
$request->setASINList($asin_list);
Your request.ASINList needs to be assigned to an ASINListType. So instantiate that object, and assign your ASINs to it's ASIN property. This is just one way of doing it, but I typically do it very quickly this way:
var asinListType = new ASINListType();
asinListType.ASIN = new List<string> { "B00005TQI7", "B00AVO5XRK", etc, etc };
request.ASINList = asinListType;
This is my code:
AccountMenuList = new[]
{
new {transKey = "MY_TICKETS", stateName="account.tickets", displayUrl="/account/tickets/"},
new {transKey = "TRANSACTION_HISTORY", stateName = "account.transactionhistory", displayUrl = "/account/transactions"},
new {transKey = "PAYIN", stateName = "account.payin",displayUrl = "/account/payin"},
new {transKey = "PAYOUT", stateName = "account.payout", displayUrl = "/account/payout"},
new {transKey = "TICKET_PAYOUT", stateName = "account.ticketpayout", displayUrl = "/account/ticketpayout"},
new {transKey = "SETTINGS",stateName="default",displayUrl="default",SubMenuList=new[]{new{transKey = "something"}}}
};
But I get the following error:
No best type found for implicitly-typed array
What's wrong? Can I fix this?
What is wrong?
For every anonymous type compiler creates an actual type behind the scenes, and all of your objects having same declaration are of one type, except of the last one:
new {transKey = "SETTINGS",stateName="default",displayUrl="default",SubMenuList=new[] {new{transKey = "something"}}
How to fix it?
Create custom type for your menu items which makes more sense as later you may want to dynamically add/remove/filter them.
By adding object to your array initialization you can explicitly create an array of objects (with different anonymous type instances in it):
var accountMenuList = new object[]
{
new {transKey = "MY_TICKETS", stateName="account.tickets", displayUrl="/account/tickets/"},
new {transKey = "TRANSACTION_HISTORY", stateName = "account.transactionhistory", displayUrl = "/account/transactions"},
new {transKey = "PAYIN", stateName = "account.payin",displayUrl = "/account/payin"},
new {transKey = "PAYOUT", stateName = "account.payout", displayUrl = "/account/payout"},
new {transKey = "TICKET_PAYOUT", stateName = "account.ticketpayout", displayUrl = "/account/ticketpayout"},
new {transKey = "SETTINGS",stateName="default",displayUrl="default",SubMenuList=new[]{new{transKey = "something"}}}
};
Remove the SubMenuList=new[]{new{transKey = "something"}} from the last entry. All entries must have the same type of data for implicitly-typed arrays to work.
Like others have pointed out. In this situation you may want to create a class.
Here is an example.
public class mydata
{
public string transKey { get; set; }
public string stateName { get; set; }
public string displayUrl{ get; set; }
}
And then I'd use a list of type.
List<mydata> myDataList = new List<mydata>();
To add to the list you can do like this.
// create object
mydata md = new mydata();
md.transKey = "MY_TICKETS";
md.stateName = "account.tickets";
md.displayUrl = "/account/tickets/";
// add to your list
myDataList.add(md);
And if you want to loop through myDataList you can do the following.
foreach(var items in myDataList)
{
response.write(items.transkey);
}
I hope this helps.
Can you help me to resolve this proplem. I will use JSON with Name and Description and want to merge two array.
var listCateogries = categories.Select(x => new
{
Name = x.Name,
Description = x.Description
});
var listProducts = products.Select(x => new
{
Name = x.Name,
Description = x.Details
});
var data = listCateogries + listProducts;
Thanks you very much.
Try Enumerable.Concat
var data = listCateogries.Concat(listProducts);
I would like to split the string below by using regex expression
Country:Subdivision, Level1:{Level2a:{Level3a, Level3b}, Level2b}
into form of
Country
Subdivision
Level1
Level2a
Level3a
Level3b
Level2b
I knew there will be a recursive function to split to string into the above form.
I'm using .net, and want to split to string into a class
public class ListHierarchy
{
public string Name { get; set; }
public ListHierarchy ParentListHierarchy { get; set; }
}
The concept as below (Output):
var list1 = new ListHierarchy() { Name = "Country" };
var list2 = new ListHierarchy() { Name = "Subdivision", ParentListHierarchy = list1 };
var list3 = new ListHierarchy() { Name = "Level1" };
var list4 = new ListHierarchy() { Name = "Level2a", ParentListHierarchy = list3 };
var list5 = new ListHierarchy() { Name = "Level2b", ParentListHierarchy = list3 };
var list6 = new ListHierarchy() { Name = "Level3a", ParentListHierarchy = list4 };
var list7 = new ListHierarchy() { Name = "Level3b", ParentListHierarchy = list4 };
Guys, I have to solution already, but still need to fine tune on the regex
public static Dictionary<string, string> SplitToDictionary(string input, string regexString)
{
Regex regex = new Regex(regexString);
return regex.Matches(input).Cast<Match>().ToDictionary(x => x.Groups[1].Value.Trim(), x => x.Groups[2].Value.Trim());
}
string input = "Country:Subdivision, Level1:{Level2a:{Level3a:Level4a, Level3b}, Level2b}";
var listHierarchy = new List<ListHierarchy>();
Dictionary<string, string> listParent = SplitToDictionary(input, #"([\w\s]+):(([\w\s]+)|([\w\s\,\{\}\:]+))");
but, i getting
{Level2a:{Level3a, Level3b}, Level2b}
rather than
Level2a:{Level3a, Level3b}, Level2b
I love regular expressions, but for this problem they are just not the right tool.
Irony is an awesome and very easy to use library that will let you write a parser for your json-like thing.
It's free, open source, and the examples include a json parser that you can adapt to your needs.
you can use this regex
([^\s,:{}])+
This would get you the Country Subdivision Level1 Level2a Level3a Level3b Level2b.
you would have to put it into an array and then push it out according to your style.
EDIT
This would actively destroy the JSON hierarchy therefore is not recommended to be used for this question/situation. This would only return strings that can be stored in an array.