I have a model like this that includes the OwnerProductRegistration and AttachmentList.
public class OwnerProductRegistration
{
public string CustomerFirstName { get; set; }
public string CustomerPhoneMobile { get; set; }
public List<AttachmentList> AttachmentLists { get; set; }
}
public class AttachmentList
{
public string FileName { get; set; }
public string Description { get; set; }
}
OwnerProductRegistration model = new OwnerProductRegistration();
AttachmentList attachmentList = new AttachmentList();
model.CustomerFirstName = "Test";
model.CustomerPhoneMobile = "1234567890";
attachmentList.FileName = "FileNameTest";
attachmentList.Description = "foo";
I want to send the entire 'OwnerProductRegistration' model with the AttachmentList data included. When I check the value contents of model, it shows the AttachmentList as null. How do I include the AttachmentList data with the model?
You must first instantiate the list on your model property, then add the attachment list to it. You can accomplish both like so:
model.CustomerFirstName = "Test";
model.CustomerPhoneMobile = "1234567890";
attachmentList.FileName = "FileNameTest";
attachmentList.Description = "foo";
model.AttachmentLists = new List<AttachmentList> { attachmentList };
If you don't want to use a collection initializer, you can break the operation up like this:
model.AttachmentLists = new List<AttachmentList>();
model.AttachmentLists.Add(attachmentList);
You're not setting the AttachmentLists property anywhere. Try this, it's similar to yours, but the property is set in the last line.
public class OwnerProductRegistration
{
public string CustomerFirstName { get; set; }
public string CustomerPhoneMobile { get; set; }
public List<AttachmentList> AttachmentLists { get; set; }
}
public class AttachmentList
{
public string FileName { get; set; }
public string Description { get; set; }
}
OwnerProductRegistration model = new OwnerProductRegistration();
AttachmentList attachmentList = new AttachmentList();
model.CustomerFirstName = "Test";
model.CustomerPhoneMobile = "1234567890";
attachmentList.FileName = "FileNameTest";
attachmentList.Description = "foo";
model.AttachmentLists = new List<AttachmentList> { attachmentList };
You can as well use Object Initializer and Collection initializer syntax available from C#3 like below
OwnerProductRegistration model = new OwnerProductRegistration
{
CustomerFirstName = "Test",
CustomerPhoneMobile = "1234567890",
AttachmentLists = new List<AttachmentList>
{
new AttachmentList
{
FileName = "FileNameTest",
Description = "foo",
}
}
}
Related
I have a ViewModel where objects are all string and the model from the database Address is a list. I wanted to know if there is any other way to set the viewModel string values other then the way I have it done below? Any advice on how I can combine the two ListOfProducts.Select lines?
static void Main(string[] args)
{
//EF Data
List<Address> ListOfProducts = new List<Address>();
ListOfProducts.Add(new Address() { StreetLine1="address 1",StreetLine2="address line2 1" });
ViewModelAddress vm = new ViewModelAddress();
vm.StreetLine1 = ListOfProducts.Select(a => a.StreetLine1).FirstOrDefault();
vm.StreetLine2 = ListOfProducts.Select(a => a.StreetLine2).FirstOrDefault();
}
class Address
{
public string StreetLine1 { get; set; }
public string StreetLine2{ get; set; }
}
class ViewModelAddress
{
public string StreetLine1 { get; set; }
public string StreetLine2 { get; set; }
}
var vm = ListOfProducts.Select(a => new ViewModelAddress
{StreetLine1 = a.StreetLine1, StreetLine2 = a.StreetLine2}).FirstOrDefault();
I have a block of code that preps a query here:
var AssetTagParam = new SQLiteParameter("#AssetTagParam", SqlDbType.Int) { Value = item.AssetTag };
var VendorParam = new SQLiteParameter("#VendorParam", SqlDbType.Text) { Value = item.Vendor };
var DeviceParam = new SQLiteParameter("#DeviceParam", SqlDbType.Text) { Value = item.Device };
var AttributeParam = new SQLiteParameter("#AttributeParam", SqlDbType.Text) { Value = item.Attribute };
var DeviceTypeParam = new SQLiteParameter("#DeviceTypeParam", SqlDbType.Text) { Value = item.DeviceType };
var SystemParam = new SQLiteParameter("#SystemParam", SqlDbType.Text) { Value = item.System };
var LocationParam = new SQLiteParameter("#LocationParam", SqlDbType.Text) { Value = item.Location };
var OnLoanParam = new SQLiteParameter("#OnLoanParam", SqlDbType.Binary) { Value = item.OnLoan };
var NotesParam = new SQLiteParameter("#NotesParam", SqlDbType.Text) { Value = item.Notes };
var LastModifiedTimeParam = new SQLiteParameter("#LastModifiedTimeParam", SqlDbType.Text) { Value = item.LastModifiedTime };
var LastModifiedPersonParam = new SQLiteParameter("#LastModifiedPersonParam", SqlDbType.Text) { Value = item.LastModifiedPerson };
var IsDeletedParam = new SQLiteParameter("#IsDeletedParam", SqlDbType.Binary) { Value = item.IsDeleted };
SQLiteCommand insertSQL = new SQLiteCommand("INSERT INTO Inventory(AssetTag, Vendor, Device, Attribute, DeviceType, System, Notes, OnLoan, Location)" +
" VALUES (#AssetTagParam, #VendorParam, #DeviceParam, #AttributeParam, #DeviceTypeParam, #SystemParam, #NotesParam, #OnLoanParam, #LocationParam)", conn);
insertSQL.Parameters.Add(AssetTagParam);
insertSQL.Parameters.Add(VendorParam);
insertSQL.Parameters.Add(DeviceParam);
insertSQL.Parameters.Add(AttributeParam);
insertSQL.Parameters.Add(DeviceTypeParam);
insertSQL.Parameters.Add(SystemParam);
insertSQL.Parameters.Add(LocationParam);
insertSQL.Parameters.Add(OnLoanParam);
insertSQL.Parameters.Add(NotesParam);
insertSQL.Parameters.Add(LastModifiedTimeParam);
insertSQL.Parameters.Add(LastModifiedPersonParam);
insertSQL.Parameters.Add(IsDeletedParam);
For Reference, the Item class looks like this:
public class Item
{
public int AssetID { get; set; }
public int AssetTag { get; set; }
public string Vendor { get; set; }
public string Device { get; set; }
public string Attribute { get; set; }
public string DeviceType { get; set; }
public string System { get; set; }
public string Location { get; set; }
public bool OnLoan { get; set; }
public string Notes { get; set; }
public string LastModifiedTime { get; set; }
public string LastModifiedPerson { get; set; }
public bool IsDeleted { get; set; }
}
When I run this code, I will always run into the generic error:
Input string was not in a correct format.
After trying to figure out the source of this issue, assuming that I typed something wrong in the process, I ran into the issue that all my Param vars had their dbType set to Int32. I thought I was setting with the parameter SqlDbType.Text, but I must be misunderstanding this?
How do I set the dbtype of my inputs to Text instead of Int32?
The mistake I was making is using SqlDbType to define the type of data. I should have been using System.Data.DbType to define the data, so this:
var VendorParam = new SQLiteParameter("#VendorParam", SqlDbType.Text) { Value = item.Vendor };
Should instead be changed to this:
var VendorParam = new SQLiteParameter("#VendorParam", System.Data.DbType.String) { Value = item.Vendor };
public class kingdomAddModel
{
public string title { get; set; }
public string details { get; set; }
//public HttpPostedFileBase fileUpload { get; set; }
//public string retrieveFile { get; set; }
public FileAttr files { get; set; }
}
public class FileAttr
{
public HttpPostedFileBase fileUpload { get; set; }
public string retrieveFile { get; set; }
}
var getDailyDevotions = db.DailyDevotions.Select(d => new { title = d.DevotionsTitle, details = d.DevotionsDetails, retriveFileAudio = d.VoiceNotes });
List<kingdomAddModel> listdevotions = new List<kingdomAddModel>();
foreach (var getDevotions in getDailyDevotions)
{
kingdomlist = new kingdomAddModel();
kingdomlist.title = getDevotions.title;
kingdomlist.details = getDevotions.details;
fileattr = new FileAttr();
fileattr.retrieveFile = getDevotions.retriveFileAudio;
kingdomlist.files.retrieveFile = fileattr.retrieveFile; //erros appears here!
}
The line line kingdomlist.files.retrieveFile throws the exception, tried googling but I dont get simular problem. I just want to assign the value and will pull on my view.
Do not access properties of FileAttr directly, only use files with the instance of kingdomAddModel. Don't mixup them
Replace
foreach (var getDevotions in getDailyDevotions)
{
kingdomlist = new kingdomAddModel();
kingdomlist.title = getDevotions.title;
kingdomlist.details = getDevotions.details;
fileattr = new FileAttr();
fileattr.retrieveFile = getDevotions.retriveFileAudio;
kingdomlist.files.retrieveFile = fileattr.retrieveFile; //erros appears here!
}
with
foreach (var getDevotions in getDailyDevotions)
{
kingdomlist = new kingdomAddModel
{
title = getDevotions.title,
details = getDevotions.details,
files = new FileAttr
{
retrieveFile = getDevotions.retriveFileAudio,
//fileUpload = some value here
}
};
listdevotions.Add(kingdomlist);
}
OR use Linq
listdevotions = (from getDevotions in getDailyDevotions
select new kingdomAddModel
{
title = getDevotions.title,
details = getDevotions.details,
files = new FileAttr
{
retrieveFile = getDevotions.retriveFileAudio,
//fileUpload = some value here
}
}).ToList();
Im trying to use Linq to select the property ProductColor from a List of MyObject into the property AllProductColors
public class MyObject
{
public string ImgUrl { get; set; }
public string ProductName { get; set; }
public string ProductColor { get; set; }
}
public class ObjectToSelectInto
{
public string ImgUrl { get; set; }
public string ProductName { get; set; }
public List<string> AllProductColors { get; set; }
}
//*** CREATING EXAMPLE ***///
List<MyObject> MyObjectList = new List<MyObject>();
ObjectToSelectInto destinationObject = new ObjectToSelectInto();
//I can do it like this, but then I
// would have to do this for every list item, not good!
destinationObject.AllProductColors =
MyObjectList.Select(x => x.ProductColor).toList();
//*** This fails ***///
destinationObject = MyObjectList.Select( x =>
new destinationObject {
AllProductColors = x.ProductColor.ToList(),
ImgUrl = x.ImgUrl.First().toString(),
ProductName = x.ProductName .First().toString()
}
I want it to be like this.
destinationObject has a list with elements where one color equals one element.
You just need to put your first query that you tried into second like this:
destinationObject = MyObjectList.Select(x =>
new ObjectToSelectInto()
{
AllProductColors = MyObjectList.Select(y => y.ProductName).ToList(),
ImgUrl = x.ImgUrl,
ProductName = x.ProductName
}).First();
I have 2 types of string: Mer and Spl
// Example
string testMer = "321|READY|MER";
string testSpl = "321|READY|SPL";
Then I will split them:
var splitMer = testMer.Split('|');
var splitSpl = testSpl.Split('|');
I have an object to save them
public class TestObject
{
public int id { get; set; }
public string status { get; set; }
public string type { get; set; }
}
Question: How to convert the Array into the TestObject?
var converted = new TestObject
{
id = int.Parse(splitMer[0]),
status = splitMer[1],
type = splitMer[2]
};
You will need to add some error checking.
var values = new List<string> { "321|READY|MER", "321|READY|SPL" };
var result = values.Select(x =>
{
var parts = x.Split(new [] {'|' },StringSplitOptions.RemoveEmptyEntries);
return new TestObject
{
id = Convert.ToInt32(parts[0]),
status = parts[1],
type = parts[2]
};
}).ToArray();
You just need to use object initializers and set your properties.By the way instead of storing each value into seperate variables, use a List.Then you can get your result with LINQ easily.
var splitMer = testMer.Split('|');
var testObj = new TestObject();
testObj.Id = Int32.Parse(splitMer[0]);
testObj.Status = splitMer[1];
testObj.type = splitMer[2];
How about adding a Constructor to your Class that takes a String as a Parameter. Something like this.
public class TestObject
{
public int id { get; set; }
public string status { get; set; }
public string type { get; set; }
public TestObject(string value)
{
var valueSplit = value.Split('|');
id = int.Parse(valueSplit[0]);
status = valueSplit[1];
type = valueSplit[2];
}
}
Usage:
TestObject tst1 = new TestObject(testMer);
TestObject tst2 = new TestObject(testSpl);