I have 2 classes
public class Product
{
public DateTime Date { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
}
public class Campaign
{
public long CampaignId { get; set; }
public string CampaignName { get; set; }
public List<Product> Products { get; set; }
}
Code:
var campaign = new Campaign();
campaign.CampaignId = Item.CampaignId;
campaign.CampaignId = Item.CampaignId;
campaign.CampaignName = Item.CampaignName;
campaign.Products = productList;
campaignList.Add(campaign);
productList.Clear();
When I call productList.Clear(), my "campaign" deletes its campaign.Products.
How can I prevent that from happening?
campaign.Products = new List<Product>(productList);
because campaign.Products is the same reference of productList
they are both pointing to the same list , any action on one will be reflected in the other varialbe
you need to clone (make another copy of the list) by different ways as follwoing
campaign.Products = productList.GetClone();
or
campaign.Products = productList.ToList();
or
campaign.Products.AddRange(productList);
check the following url
https://www.techiedelight.com/clone-list-in-csharp/
Related
First off I am very new to LINQ.
I have a list which has data something like this :
list
list[0] = id=1,block=10,sg=320,dc=null
list[1] = id=1,block=10,sg=null,dc=320
list[2] = id=2,block=15,sg=400,dc=null
list[3] = id=2,block=15,sg=null,dc=400
I want to update this list such that :
if(sg where block=x and id=y is null)
then set sg = (sg where block=x and id=y is not null)
and similarly for dc
Desired Result:
list[0] = id=1,block=10,sg=320,dc=320
list[1] = id=2,block=15,sg=400,dc=400
NOTE: id and block are identifying factors here.
CLASS:
public class dcsg
{
public long id { get; set; }
public Nullable<decimal> dcvalue { get; set; }
public Nullable<decimal> sgvalue { get; set; }
public Nullable<int> revision { get; set; }
public Nullable<long> timestampid { get; set; }
public decimal fuelcost { get; set; }
public Nullable<short> isdeleted { get; set; }
public Nullable<long> blockno { get; set; }
public int stageid { get; set; }
}
You could achieve this using Linq, GroupBy.
lists = lists.GroupBy(x=> new {x.id, x.blockno})
.Select(x=>
{
var sg1 = x.FirstOrDefault(f=>f.sgvalue.HasValue);
var dc1 = x.FirstOrDefault(f=>f.dcvalue.HasValue);
return new dcsg() // create class instance if have one.
{
id= x.Key.id,
blockno= x.Key.blockno,
sgvalue = sg1==null? null; sg1.sgvalue,
dcvalue = dc1==null? null; dc1.dcvalue,
// copy other properties (if needed).
};
})
.ToList();
Obviously, code snippet written with two assumptions.
In case of multiple sg appear for same block first sg will betaken (but this can be changed based on need).
Based on your example, id,block are used to group list items.
I have developed my first API controlled in MVC4 and through the scaffolding I have got it to automatically output a list of items:
// GET api/ItemList
public IEnumerable<ItemOption> GetItemOptions()
{
var itemoptions = db.ItemOptions.Include(i => i.Item);
return itemoptions.AsEnumerable();
}
This shows all the item properties from my model:
public class ItemOption
{
public int ItemOptionId { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
public string test1 { get; set; }
public double PriceNet { get; set; }
}
How can I specify specific fields I wish to be returned? For example, I just want the ItemOptionId, Active and Name to be returned.
I have tried adding additional includes, but this seems to be at an object level.
Try creating a new type to represent the properties you'd like to return:
public class ItemOptionResult
{
public int ItemOptionId { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
}
And then projecting your ItemOption collection, like this:
// GET api/ItemList
public IEnumerable<ItemOptionResult> GetItemOptions()
{`enter code here`
var itemoptions =
db.ItemOptions
.Select(i =>
new ItemOptionResult
{
ItemOptionId = i.ItemOptionId,
Active = i.Active,
Name = i.Name
});
return itemoptions.AsEnumerable();
}
Try this :
var itemoptions = db.ItemOptions.Select(io => new ItemOption()
{
ItemOptionId = io.ItemOptionId,
Active = io.Active ,
Name = io.Name
}
return itemoptions.AsEnumerable();
I'm using c#
one of the mongo db document is in such structure
Class QuestionData{
public Guid Id { get; set; }
public string Type { get; set; }
public List<NotesQuestion> Question { get; set; }
}
So I want to do a updating. I write the update as following:
var update = Update.Set("Question", data.Question);
The 'data' is a type of QuestionData.
But now it says "invalid argument" of data.Question.
If I change it to
var update = Update.Set("Question", data.Question.ToJson());
It has no problem. But I don't want to save it as json string.
How to resolve this issue?
With my own model classes,
internal class QuestionData
{
[BsonId] public Guid Id { get; set; }
[BsonElement("type")] public string Type { get; set; }
[BsonElement("qnotes")] public QuestionNote[] QuestionNotes { get; set; }
}
[BsonNoId] internal class QuestionNote
{
[BsonElement("q")] public string Question { get; set; }
[BsonElement("n")] public string Note { get; set; }
}
you can overwrite the whole bson array like:
// get client, database, collection ...
var col = Database.GetCollection<QuestionData>("questions");
// QuestionNote[] toModifyWith = ...
var udb = Builders<QuestionData>.Update;
var fdb = Builders<QuestionData>.Filter;
var ur = await col.UpdateManyAsync(fdb.Empty, udb.Set(x => x.QuestionNotes, toModifyWith ));
I have an application that will parse an excel file and add a column, then generate a new CSV file with the results. I am able to create a list of the items I want in the file, but I cannot figure out how to pass that list to the method that is generating the new file.
I have the following class:
public class LocationData
{
public string PostalCode { get; set; }
public string Partner { get; set; }
public string LocationID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Market { get; set; }
}
and the following code to get the data into a list:
LocationData Locationdata = new LocationData()
{
PostalCode = location[0],
Partner = location[1],
LocationID = location[2],
Name = location[3],
Country = location[4],
Market = repository.GetMarketsForPostalCode(location[0])
}
I also have the method to create the csv and I need to pass in the list info, but I get the error:
foreach statement cannot operate on variables of type 'app.LocationData' because 'app.LocationData' does not contain a public definition for 'GetEnumerator'
I think you are misunderstanding what a list is in C#. I think you need the List data type. Try this:
List<string> Locationdata = new List<string>()
{
location[0],
location[1],
location[2],
location[3],
location[4],
repository.GetMarketsForPostalCode(location[0])
};
Your csv function will look like this
public void GenerateCSV(List<LocationData> data)
{
foreach (LocationData d in data)
{
//put line in csv as
string s = d.PostalCode + "," d.Partner + _"," + d.LocationID...... + Environment.NewLine;
}
}
Your class declaration will remain same
public class LocationData
{
public string PostalCode { get; set; }
public string Partner { get; set; }
public string LocationID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Market { get; set; }
}
Now you need to add all the data in the list first
which you will do like this
List<LocationData> lst = new List<LocationData>();
LocationData ld = new LocationData();
ld.LocationID = "0";
ld.Market = "market";
lst.Add(ld);
........
GenerateCSV(lst);
I'm having some trouble storing and retrieving items into a list<> with a custom structure.
My structure looks like this:
public class list_rss_parameters
{
public string this_string { get; set; }
public string title_start { get; set; }
public string title_end { get; set; }
public string description_start { get; set; }
public string description_end { get; set; }
public string link_start { get; set; }
public string link_end { get; set; }
public string publish_date_start { get; set; }
public string publish_date_end { get; set; }
public string author_start { get; set; }
public string author_end { get; set; }
}
My stored procedure looks like this (and note that the variable names are the same as the custom Key names) Is this ok?
//this is the last part of a custom method that returns a list
List<list_rss_parameters> list_rss_items = new List<list_rss_parameters>();
list_rss_items.Add(new list_rss_parameters()
{
this_string = this_string,
title_start = title_start,
title_end = title_end,
description_start = description_start,
description_end = description_end,
link_start = link_start,
link_end = link_end,
publish_date_start = publish_date_start,
publish_date_end = publish_date_end,
author_start = author_start,
author_end = author_end
});
return list_rss_items;
If the above two setups are correct, how do I pull items out of the List once I return it?
List<list_rss_parameters> list_rss_parameters = new List<list_rss_parameters>();
list_rss_parameters = f_discover_rss_parameters(rss);
show(list_rss_parameters.Count.ToString());
show(list_rss_parameters[0].ToString()); //does not show this_string
show(list_rss_parameters[this_string'] //does not show this_string
show(list_rss_parameters[0][this_string'];//does not show this_string
What am I doing wrong?
You want the this_string property of the first item in your list it seems:
show(list_rss_parameters[0].this_string);
Or show all of them:
foreach(var item in list_rss_parameters)
{
Console.WriteLine(item.this_string);
}
As a side note your property names don't match the PascalCase naming convention for properties in .NET - so this_string really should be ThisString.