I have a collection defined by:
public class CompanyModel
{
public string compnName { get; set; }
public string compnAddress { get; set; }
public string compnKeyProcesses { get; set; }
public string compnStandards { get; set; }
}
Then I stored names and addresses to this collection from a data table:
List<CompanyModel> companies = new List<CompanyModel>();
for(int i = 0; i < dt.Rows.Count; i++)
{
companies.Add(new CompanyModel
{
compnName = dt.Rows[i]["companyName"].ToString(),
compnAddress = dt.Rows[i]["address"].ToString()
});
}
My question is how could I retrieve each compnName from that collection ?
I tried this
foreach (CompanyModel company in companies)
{
string compnyName = company.compnName;
But it return me blank result.
The simplest option would be to use LINQ, e.g.
var names = companies.Select(c => c.compnName);
Related
i have my dto
public class DocumentForListDto
{
public int Id { get; set; }
public string Title { get; set; }
public string SubmittedBy { get; set; }
public DateTime SubmittedAt { get; set; }
public List<AuditsUpdateForListDto> UpdatedDocuments { get; set; }
}
public class AuditsForListDto
{
public string FullName { get; set; }
public DateTime UpdatedAt { get; set; }
}
and this code in my controller :
var docs = await _repo.Doc.Get();
and i have this audit to save any action in database
var aud = await _repo.Audit.FindByPrimaryKey(Constants.Doc, documents.Select(x => x.Id).ToList());
and this mapper for map my doc to dto(content)
var contents = _mapper.Map<IEnumerable<DocumentForListDto>>(docs);
and this my foreach to bind from audit to contents (CreatedBy/CreatedAt && UpdateBy/UpdatedAt)
if (contents.Any() && contents.Count() > 0 && audits.Any() && audits.Count()
> 0)
{
foreach (var content in contents)
{
//Search here by create Action
foreach (var audit in audits.Where(x =>
Convert.ToInt32(Regex.Match(x.PrimaryKey, #"\d+").Value) ==
content.Id && x.Type.Equals(Constants.Create)))
{
content.SubmittedBy = string.Concat(audit.User.FirstName, " ",
audit.User.LastName);
content.SubmittedAt = audit.DateTime;
}
}
//Here I need to bind list of Updated By and Updated At But I try many times but I don't find the right solution
}
i need to bind list of Updated By and Updated At i try with many logics but without success ??
I have a C# project and looking for simple solution for map one class object data to list of another class object.
This is my input class
public class RatesInput
{
public string Type1 { get; set; }
public string Break1 { get; set; }
public string Basic1 { get; set; }
public string Rate1 { get; set; }
public string Type2 { get; set; }
public string Break2 { get; set; }
public string Basic2 { get; set; }
public string Rate2 { get; set; }
public string Type3 { get; set; }
public string Break3 { get; set; }
public string Basic3 { get; set; }
public string Rate3 { get; set; }
}
This is my another class structure
public class RateDetail
{
public string RateType { get; set; }
public decimal Break { get; set; }
public decimal Basic { get; set; }
public decimal Rate { get; set; }
}
it has a object like below. (For easiering the understanding, I use hardcoded values and actually values assign from a csv file)
RatesInput objInput = new RatesInput();
objInput.Type1 = "T";
objInput.Break1 = 100;
objInput.Basic1 = 50;
objInput.Rate1 = 0.08;
objInput.Type2 = "T";
objInput.Break2 = 200;
objInput.Basic2 = 50;
objInput.Rate2 = 0.07;
objInput.Type3 = "T";
objInput.Break3 = 500;
objInput.Basic3 = 50;
objInput.Rate3 = 0.06;
Then I need to assign values to "RateDetail" list object like below.
List<RateDetail> lstDetails = new List<RateDetail>();
//START Looping using foreach or any looping mechanism
RateDetail obj = new RateDetail();
obj.RateType = //first iteration this should be assigned objInput.Type1, 2nd iteration objInput.Type2 etc....
obj.Break = //first iteration this should be assigned objInput.Break1 , 2nd iteration objInput.Break2 etc....
obj.Basic = //first iteration this should be assigned objInput.Basic1 , 2nd iteration objInput.Basic2 etc....
obj.Rate = //first iteration this should be assigned objInput.Rate1, 2nd iteration objInput.Rate2 etc....
lstDetails.Add(obj); //Add obj to the list
//END looping
Is there any way to convert "RatesInput" class data to "RateDetail" class like above method in C#? If yes, how to iterate data set?
Try this:
public class RatesList : IEnumerable<RateDetail>
{
public RatesList(IEnumerable<RatesInput> ratesInputList)
{
RatesInputList = ratesInputList;
}
private readonly IEnumerable<RatesInput> RatesInputList;
public IEnumerator<RateDetail> GetEnumerator()
{
foreach (var ratesInput in RatesInputList)
{
yield return new RateDetail
{
RateType = ratesInput.Type1,
Break = Convert.ToDecimal(ratesInput.Break1, new CultureInfo("en-US")),
Basic = Convert.ToDecimal(ratesInput.Basic1, new CultureInfo("en-US")),
Rate = Convert.ToDecimal(ratesInput.Rate1, new CultureInfo("en-US"))
};
yield return new RateDetail
{
RateType = ratesInput.Type2,
Break = Convert.ToDecimal(ratesInput.Break2),
Basic = Convert.ToDecimal(ratesInput.Basic2),
Rate = Convert.ToDecimal(ratesInput.Rate2, new CultureInfo("en-US"))
};
yield return new RateDetail
{
RateType = ratesInput.Type3,
Break = Convert.ToDecimal(ratesInput.Break3),
Basic = Convert.ToDecimal(ratesInput.Basic3),
Rate = Convert.ToDecimal(ratesInput.Rate3, new CultureInfo("en-US"))
};
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
And use:
var list = new RatesList(new List<RatesInput>() { objInput });
foreach (var item in list)
{
Console.WriteLine(item.Basic);
}
You can use Reflection to get the properties info like this:
var props = objInput.GetType().GetProperties();
var types = props.Where(x => x.Name.StartsWith("Type"))
.Select(x => x.GetValue(objInput)).ToList();
var breaks = props.Where(x => x.Name.StartsWith("Break"))
.Select(x => x.GetValue(objInput)).ToList();
var basics = props.Where(x => x.Name.StartsWith("Basic"))
.Select(x => x.GetValue(objInput)).ToList();
var rates = props.Where(x => x.Name.StartsWith("Rate"))
.Select(x => x.GetValue(objInput)).ToList();
List<RateDetail> lstDetails = new List<RateDetail>();
for (int i = 0; i < types.Count; i++)
{
lstDetails.Add(new RateDetail
{
RateType = types[i].ToString(),
Break = Convert.ToDecimal(breaks[i]),
Basic = Convert.ToDecimal(basics[i]),
Rate = Convert.ToDecimal(rates[i])
});
}
I'm trying to pass my CookieCarts string array (containing shopping cart items) into my controller to get looped for my Paypal api.
My View
var cookiecart = Server.UrlDecode(Request.Cookies["cookieCart"].Value);
#Html.HiddenFor(m => m.CookieCart, new { Value = cookiecart })
Response.Write(cookiecart);
cookiecart:*[{"datetime":"2016-02-25 02:51:49","id":"749","typeid":"13","qty":1,"fullname":"The Matrix","image":"/Content/images/products/online-video.png","price":"69","sku":"MATRIX"}]*
My Model
public string CookieCart { get; set; }
My Controller
var cartArray = model.CookieCart;
var cartArray = model.CookieCart;
var itemArray = cartArray.Split(',');
foreach (var t in itemArray)
{item.name = itemArray[0]; }
when i quickwatch the data sent to the controller it looks like this:
cartArray displays: "[{\"datetime\":\"2016-02-25 02:51:49\",\"id\":\"749\",\"typeid\":\"13\",\"qty\":1,\"fullname\":\"The Matrix\",\"image\":\"/Content/images/products/online- video.png\",\"price\":\"69\",\"sku\":\"MATRIX\"}]"
item.name displays: *"[{\"datetime\":\"2016-02-25 02:51:49\""*
None of this is right. its so frustrating! How to convert a cookie array value into a C# array.
itemArray[0] should be:
itemArray[0][0] = datetime:"2016-02-25 02:51:49",
itemArray[0][1] = id:"749",
itemArray[0][2] = typeid:"13",
itemArray[0][3] = qty:1,
itemArray[0][4] = fullname:"The Matrix",
itemArray[0][5] = image:"/Content/images/products/online-video.png",
itemArray[0][6] = price:"69"
itemArray[0][7] = sku:"MATRIX"
:(
ok i figured it out. using JSON .Net:
My Controller
var cookie = Request.Cookies["cookieCart"];
cookieArray = JsonConvert.DeserializeObject<List<CookieCart>>
(Server.UrlDecode(cookie.Value));
My Model
public class CookieCart
{
public DateTime Datetime { get; set; }
public int Id { get; set; }
public int Typeid { get; set; }
public string Qty { get; set; }
public string Fullname { get; set; }
public string Image { get; set; }
public string Price { get; set; }
public string Sku { get; set; }
}
then i iterated the array items for PayPal:
foreach (var cartitem in cookiecart)
{
item.name = cartitem.Fullname;
item.currency = "USD";
item.price = cartitem.Price;
item.quantity = cartitem.Qty;
item.sku = cartitem.Sku;
var intPrice = Int32.Parse(cartitem.Price);
subtotal = subtotal + intPrice;
}
Im trying to do a single linq statement, the following works but want to do it within a single statement.
public class ClientProducts
{
public string To { get; set; }
public string ClientFullName { get; set; }
public string ClientFirstName { get; set; }
public string ProductNames{ get; set; }
}
var list =
clients.Select(
client =>
new ClientProducts()
{
To = client.TelephoneMobile,
ClientFirstName = client.FirstName,
ClientFullName = client.FullName,
//ProductNames= client.Products.Select(p=>p.Name)<-this is what I want
}).ToList();
string productName= string.Empty;
foreach (var client in clients)
{
foreach (var p in client.Products)
{
productName+= Name+ ",";
}
}
replace
//ProductNames= client.Products.Select(p=>p.Name)
with
ProductNames = string.Join(",", client.Products.Select(p=>p.Name))
IList<ReceiptAllocationMaster> objReceiptMaster = (IList<ReceiptAllocationMaster>)Session["AllocationResult"];
public class ReceiptAllocationMaster
{
public string application { get; set; }
public List<Users> users { get; set; }
}
public class Users
{
public string name { get; set; }
public string surname { get; set; }
}
I need to Update the above list with some value where application = "applicationame" and users where surname = "surname" into the same list.
Just iterate over your list and modify matched items:
for (int i = 0; i < objReceiptMaster.Count; i++)
{
var item = objReceiptMaster[i];
if (item.application == "applicationname" && item.users.Any(x => x.surname == "surname"))
objReceiptMaster[i] = new ReceiptAllocationMaster();
}
Instead of new ReceiptAllocationMaster() you can write any modification data logic.
though your question is not clear, this shd give u some idea:
objReceiptMaster.Where(x=>x.application=="applicationname" &&
x.users.Any(d=>d.surname=="surname"))
.ToList()
.ForEach(item=>{//update your list
item.application = "whatever value";
item.users.ForEach(user=>{//update users
user.name="whatever username";
});
});