Add values in list from other API method - c#

I have one list and in that list I am adding values based on class. See below for details.
result.ContactList = contactsResult.Item2.Select(x => new PrjContact() {
Id = x.ID,
UserId = x.UserId,
Name = xName,
Email = x.Email,
}).ToList();
Now I need to call one more API and pass this UserId in that API and get phone number for that user.
Then need to add that phone number in above list in result.ContactList.
I have tried in this way.
foreach (var user in contactsResult.Item2)
{
UserInfo = API.GetUserDetail(user.UserId);
result.ContactList.Select(x => new ProjectContactView()
{
Phone = UserInfo.PhoneNumber
});
}
But this is not working.

This doesn't do anything:
result.ContactList.Select(x => new ProjectContactView()
{
Phone = UserInfo.PhoneNumber
});
Sure, it iterates over result.ContactList and projects it into a new collection. But (1) you don't do anything with that collection and (2) it overwrites every object in that collection with an entirely new object that has only one property set.
For starters, if you want to modify result.ContactList then iterate over that:
foreach (var user in result.ContactList)
{
}
Then if the goal here is to use a property on user to fetch data and update user then just update user:
foreach (var user in result.ContactList)
{
var userInfo = API.GetUserDetail(user.UserId);
user.Phone = userInfo.PhoneNumber
}

Related

How to map child entities in a reusable manner?

I need to map an EF entity to an entity more suitable for performing business logic operations. To do so, I am using this expression:
var phoneMapper = phone => new BasePhone
{
Id = phone.Id,
Number = phone.Number,
PhoneType = phoneTypeMapper(phone.PhoneType) //Pseudo-Code
}
In this example, the phone type class should have a mapping of its own:
var phoneTypeMapper = phoneType => new BasePhoneType
{
Id = phoneType.Id,
Name = phoneType.Name
}
I want to use phoneTypeMapper within the definition of phoneMapper, how can I do so? I know this is easy to do with a collection:
var phoneMapper = phone => new BasePhone
{
Id = phoneType.Id,
Name = phoneType.Name,
//Now a phone has many phone types
PhoneTypes = phone.PhoneTypes.Select(phoneTypeMapper)
}
Is there a way to do this for a non-enumerable property?
EDIT
I should also mention that I would like for this to not collapse the query until the parent query has collapsed. I.E. I want to just jam this into a select and not have it hit the database every time it needs to pull record status data.

part of String contained in a List

Here's what I'm trying to do:
Create a list with some values from mysql.
Search this list with a variable ( I named it Existed )
If Existed contains a specific string, then do some actions.
Here's a sample of my list data:
List ( name users )
Facebook
Google
Yahoo
Strongman
Zombies
Stratovarius
If Existed inside users contains Strong, then perform some action.
My code so far is below. The problem is that it never enters the action and for some reason I believe it does not see "Strong" right.
List<string> users = dbm.FindManagers();
foreach (var Existed in users)
{
if (Existed.Contains(rName_Add_User_result))
{
dbm.AddSubuser(Existed, rName_result);
}
}
Can't reproduce. This works for me:
var rName_Add_User_result = " Strong ";
//List<string> users = dbm.FindManagers();
var users = new List<string>() {"Facebook", "Google", "Yahoo", "Strongman", "Zombies", "Stratovarius"};
foreach (var Existed in users.Where(u => u.ToUpper().Contains(rName_Add_User_result.ToUpper().Trim()))
{
//dbm.AddSubuser(Existed, rName_result);
Console.WriteLine(Existed);
}
Result:
Strongman
Not sure but could be because of case sensitivity. Try converting it to lower and then compare
if (Existed.ToLower().Contains(rName_Add_User_result))
{
dbm.AddSubuser(Existed, rName_result);
}

Getting the profileid for a userid

In our company we created a custom Issues app. Additionally to using this app in the web interface, we also want to be able to change the state of an issue (new, acknowledged, test, resolved, ...) automatically via git commit hooks. The basics are working fine (ie change state, add notes, ...), but we also want to change the responsibility for the current item to a specific user. In that special case, it's the creator if this item.
My first try was the following:
var appid = 1234; var itemid = 1;
var item = podio.ItemService.GetItemByAppItemId(appid, itemid);
var update = new Item {ItemId = item.ItemId};
var creator = item.CreatedBy.Id;
var resp = update.Field<ContactItemField>("responsibility");
resp.ContactIds = new List<int>{creator.Value};
//change some other fields as well
podio.ItemService.UpdateItem(update);
This throws an "Object not found" exception, because in the resp.ContactIds one must not set the UserId but the ProfileId.
I then tried to get the ProfileId of the item-creator via
podio.ContactService.GetUserContactField(creator.Value, "profile_id");
but this also throws an exception "(Authentication as app is not allowed for this method").
So how can I get an appropriate profile id for the user when I use authentication as app?
OK, I found a workaround for it, not sure, if this is possible for other scenarios, but it works for the current case.
Instead of using the C# interface for setting the ContactIds for the ContactItemField, I set the json values directly.
var appid = 1234; var itemid = 1;
var item = podio.ItemService.GetItemByAppItemId(appid, itemid);
var update = new Item {ItemId = item.ItemId};
var creator = item.CreatedBy.Id;
var resp = update.Field<ContactItemField>("responsibility");
resp.ContactIds = new List<int>(); // set to an empty list, so that resp.Values is initialized to an empty JArray
var u = new JObject { {"value", new JObject { {"type" , "user" }, {"id", creator } } } };
responsibleField.Values.Add(u); //add the new user to the Values of the field
//change some other fields as well
podio.ItemService.UpdateItem(update);
And if I set the value with type user I can use the known userid and the API on the server takes care of the lookup.

Trouble with loops inside list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I has to use a construct like
string json = await SendGraphGetRequest("/users", null);
var jsonObj = JObject.Parse(json);
var values = (JArray)jsonObj["value"];
foreach (var value in values)
List<Users> Users = new List<Models.Users>()
{
foreach (var value in values)
{
};
};
How i have to correct the syntax?
I want to bring values from the json Obj in to the list.
Let's try to break down what you're trying to do into 3 simple steps. First you need to create a List data structure to store your users. Then you'll need to add some users to your empty list. Then finally you can enumerate your list with a for-each loop:
// First create a list of your users.
List<Model.Users> users = new List<Models.Users>();
// Add some users to the list.
users.Add(new Model.Users())
...
...
...
// Now you can loop through the list
foreach (var user in users)
{
// do something with the current user
user.DoSomething();
};
Hope this helps!
You can rewrites it with Linq
List<Users> Users = values.Select(value => new Models.Users( ... )).ToList();
Instead ... or new Models.Users( ... ) you can use your logic
Is each value in values a Users object?
If so,
List<Users> Users = new List<Models.Users>(values);
The List<Users> is initialized with the items in the values collection.
Or, perhaps each value contains some properties that you would use to construct your object:
var Users = new List<Models.Users>();
foreach (var value in values)
{
var newUser = new Users();
//Just making these up.
newUser.Name = value.Name;
newUser.Id = value.UserId;
Users.Add(newUser);
}
var Users = new List<Models.Users>();
foreach (var value in values)
{
var newUser = new Users();
//Just making these up.
newUser.Name = value.Name;
newUser.Id = value.UserId;
Users.Add(newUser);
}
The rest of these examples do the exact same thing - they're just shorthand.
var Users = new List<Models.Users>();
foreach (var value in values)
{
Users.Add(new Users{Name=value.Name, Id=value.UserId});
}
This is using Linq. It does the exact same thing as above. We use it primarily because it looks cool, but the syntax is different until you get used to it.
var Users = new List<Models.Users>(values.Select(
value=>new Users{Name=value.Name, Id=value.UserId}));
It's doing this:
var Users = new List<Models.Users>([some set of Users to initialize the collection]);
and this is the set of users being used to to initialize the collection:
values.Select(value=>new Users{Name=value.Name, Id=value.UserId})
In other words, for each item in values select (create) a new Users object.

Errors when creating a custom Querable object with MVC and Subsonic pagedlist

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();

Categories