Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The first 3 lines of code works fine..
How can I do the same when using object initializer ?
// works
Customer MyCustomerx = new Customer();
MyCustomerx.Location[0].place = "New York";
MyCustomerx.Location[1].place = "France";
// problem here
List<Customer> MyCustomer = new List<Customer>
{
new Customer() { Name= "Me",Location[0].place = "New York" }
}
There's no equivalent of that code within object initializers - you can't specify indexers like that. It's slightly unusual that it works even directly... I'd expect to have to add to a Locations property, rather than there being two already available which I could set an unconventionally-named property on. For example, this would be idiomatic:
Customer customer = new Customer {
Name = "Me",
Locations = {
new Location("New York"),
new Location("France")
}
};
(I'd probably put the name into a constructor parameter, mind you.)
You could then use that within a collection initializer, of course.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to achieve the same code logic using Linq basically or re factoring the code would be great.
List<PublisherDto> listOfMappedPublishers = new List<PublisherDto>();
listOfPublishers.ForEach(pub =>
{
PublisherDto publisher = new PublisherDto();
publisher.ApiKey = pub.ApiKey;
publisher.Name = pub.Name;
publisher.Password = pub.Password;
listOfMappedPublishers.Add(publisher);
});
return listOfMappedPublishers;
You can use Select and ToList as follow:
List<PublisherDto> listOfMappedPublishers = listOfPublishers.Select(i=> new PublisherDto()
{
ApiKey = i.ApiKey,
Name = i.Name,
Password = i.Password
}).ToList();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to refactor the code, I need to make this code dynamic, how to do that, see below part?
public class Trv34
{
public string CustomerNumber
public string ProductName
}
Models.Trv34[] itemTrv34 = new Models.Trv34[3]
itemTrv34[0] = new Models.Trv34 {CustomerNumber ="1", ProductName="p1"}
itemTrv34[1] = new Models.Trv34 {CustomerNumber ="2", ProductName="p2"}
itemTrv34[2] = new Models.Trv34 {CustomerNumber ="3", ProductName="p3"}
request.VODB.Trv34= new Models.Trv34[] {
itemTrv34[0],
itemTrv34[1],
itemTrv34[2],
};
I want to replace this part with something dynamic instead
itemTrv34[0],
itemTrv34[1],
itemTrv34[2],
The easiest way that comes to mind is to use Linq:
request.VODB.Trv34 = itemTrv34.ToArray();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a following code:
var dict = new Dictionary<byte?, List<Id>>();
foreach (var record in Records)
{
if(record.SubId.HasValue)
{
dict.Add(SubIdsDictionary[record.SubId.Value], new List<Record> { record });
}
else
{
dict.Add(IdsDictionary[record.Id.Value], new List<Record> { record });
}
}
Records is the list of records for one name.In this record class I have Id,SubId and Companyname. Each name have id as mandatory but subid is not.So,if name has subid then I have to give priority to subid and first get the new sub id from the dictionary SubIdsDictionary(because subid is key in ref dict SubIdsDictionary) and make the new subid as key and the whole record as value in the new Dictionary dict. But if name dont have subid then i will use id and first get the new id from reference dict IdsDictionary and make the new id as key and value will be whole record.
Now in the new dictionary keys will be mix of ids and subids and record will be value as List
Can someone help me to convert this code through lambda?
You can use ToDictionary method from Linq.
It should looks like this:
var dict = Records.ToDictionary(
r => (r.SubId.HasValue ? SubIdsDictionary[r.SubId.Value] : IdsDictionary[r.Id.Value]),
r => new List<Record> {r}
);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Firstly please forgive me as I am still trying to get to grips with C# and OOP.
I am trying to build a simple console shopping basket as part of a challenge and I have a number of products which I need to be able to pull on to populate 5 different scenarios of the basket.
However I am unsure as to the best approach to listing each of the products which each have three different values (Desc, Dept, Price) and I wish to be able to select the items I need through a array, possibly.
Currently I have the items listed as such:
itemOnePrice = 10.50m;
itemTwoPrice = 54.65m;
itemThreePrice = 03.50m;
itemOneDept = "Clothing";
itemTwoDept = "Clothing";
itemThreeDept = "Head Gear";
itemOneDesc = "Hat";
itemTwoDesc = "Jumper";
itemThreeDesc = "Head Light";
I have looked at Lists and at Tuple, but I haven't been able to figure out how to really make these work for me. Can somebody please explain the best approach to list these products to pull from to populate my basket contents.
First, create a class
class Item
{
public decimal Price {get;set;}
public string Department {get;set;}
public string Description {get;set;}
}
Second, create the list
List<Item> items = new List<Item>();
items.Add(new Item{Price = 10.5m, Department = "Clothing", Description = "Hat"});
items.Add(new Item{Price = 54.65m, Department = "Clothing", Description = "Jumper"});
items.Add(new Item{Price = 03.50m, Department = "Head Gear", Description = "Head Light"});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to generate a random number from first list (list of Object) and put it in the second list to get a random connection id to make connection between the original id and the random id how I can get the item from the first list by index and of which type I have to cast it
public class OneHub :Hub
{
static List<UserId> ConnectedUser = new List<UserId>();
static List<MessageDetail> CurrentMessage = new List<MessageDetail>();
static List<ConnectionsId> Connection = new List<ConnectionsId>();
public void Connect(string id)
{
if (ConnectedUser.Count(x => x.ConnectionId == id) == 0)
{
ConnectedUser.Add(new UserId { ConnectionId = id });
if (ConnectedUser.Count != 0 || ConnectedUser.Count != 1)
{
Random r = new Random();
int x = r.Next(0,ConnectedUser.Count);
(object)ConnectedUser.item[x];
Connection.Add(new ConnectionsId {ConnectionId=id,ConnectionId2= })
}}}
First off, you're going to need to make sure that the ConnectedUser that you randomly get is not the same user you are linking to, before you add that connection, or you're going to find further issues.
For ConnectedUser, you can get the index by simply using ConnectedUser[x]. (I suggest making your lists plural so it's obvious that they're collections.)
You need to assign that connected user to a new object.
Something like
UserID linkedUser = ConnectedUser[x];
This way, you can reference linkedUser.ConnectionId in your connection addition.
Alternately, you could just use:
Connection.Add(new ConnectionsId { ConnectionId = id, ConnectionId2 = ConnectedUser[x].ConnectionId };
This random setup, though, does have a strong potential for having several people ending up not linked to anyone. Additionally, your line that states:
if (ConnectedUser.Count != 0 ...
is redundant. You just added a user to that list. It should never be of size 0.