I have class where i have the collection of FreeDressingItems, FreeToppingItems, FreeInstructionItems
that is like this each of which fill selectedCustomization i have another property in this class public string Items { get { return GetAllItems(); } }
that i want to fill so that it keeps the all catetoryname for the same category type so that i can bind it to grid easily and display all its value in comma separated form.
i have following code could somebody help me how can i acineve this.
public class selectedCustomization
{
public CategoryType TypeName { get; set; }
public string CategoryName { get; set; }
public string ItemName { get; set; }
public int SourceID { get; set; }
public string Items { get { return GetAllItems(); } }
private string GetAllItems()
{
switch (TypeName)
{
case CategoryType.Dressing:
{
cFreeCustomization cfreeCust = new cFreeCustomization();
break;
}
case CategoryType.Topping:
break;
case CategoryType.SpecialInstruction:
break;
}
}
}
this is another class cFreeCustomization
public List<selectedCustomization> SelectedItems
{
get
{
libDBDataContext cn = new libDBDataContext();
List<selectedCustomization> lst = new List<selectedCustomization>();
lst.AddRange(
(from xx in this.FreeDressingItems
select new selectedCustomization() { TypeName = CategoryType.Dressing, CategoryName = xx.DressingInfo.CatName, ItemName = xx.DressingInfo.Description }
).ToList()
);
lst.AddRange(
(from xx in this.FreeToppingItems
select new selectedCustomization() { TypeName = CategoryType.Topping, CategoryName = xx.ToppingInfo.CatName, ItemName = xx.ToppingInfo.Description }
).ToList()
);
lst.AddRange(
(from xx in this.FreeInstructionItems
select new selectedCustomization() { TypeName = CategoryType.SpecialInstruction, CategoryName = xx.InstructionInfo.CatName, ItemName = xx.InstructionInfo.Description }
).ToList()
);
return lst;
}
}
How can i make tiems of selectedCustomization in comma separated form?
I believe the method GetAllItems should be like below:
private string GetAllItems()
{
cFreeCustomization cfreeCust = new cFreeCustomization();
var ls = cfreeCust.SelectedItems.FindAll(I => I.TypeName == this.TypeName);
return string.Join(",", ls.Select(I => I.CategoryName).ToArray());
}
This will fix your issue.
Related
I'm trying to create a List of object Work using Dapper to do the mapping.
This is the code:
public class Work
{
public int id { get; set; }
public int id_section { get; set; }
public decimal price { get; set; }
public Model id_model { get; set; }
public Type id_type { get; set; }
}
class Model
{
public int id_model { get; set; }
public string Name { get; set; }
}
class Type
{
public int id_type { get; set; }
public string Name { get; set; }
}
public List<Work> GetListOfWork(int idList)
{
using (DatabaseConnection db = new DatabaseConnection()) //return a connection to MySQL
{
var par = new {Id = idList};
const string query = "SELECT id,id_section,price,id_model,id_type FROM table WHERE id_section = #Id";
return db.con.Query<Work, Model, Type, Work>(query,
(w, m, t) =>
{
w.id_model = m;
w.id_type = t;
return w;
}, par, splitOn: "id_model,id_type").ToList();
}
}
It doesn't give me any error but id_model and id_type in my the returned List are always empty (The object are created but all the fields are empty or null), other fields are mapped correctly.
Any clue ?
You need to add yourself the joins in the query string
Probably it is something like this
var par = new {Id = idList};
const string query = #"SELECT w.id,w.id_section,w.price,
m.id_model, m.Name, t.id_type, t.Name
FROM work w INNER JOIN model m on w.id_model = m.id_model
INNER JOIN type t on w.id_type = t.id_type
WHERE w.id_section = #Id";
return db.con.Query<Work, Model, Type, Work>(query,
(w, m, t) =>
{
w.id_model = m;
w.id_type = t;
return w;
}, par, splitOn: "id_model,id_type").ToList();
I am trying to populate an Objective and ObjectiveDetail objects. Here are the classes I have:
partial class Objective
{
public Objective() {
this.ObjectiveDetails = new List<ObjectiveDetail>();
}
public int ObjectiveId { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
}
public partial class ObjectiveDetail
{
public int ObjectiveDetailId { get; set; }
public int ObjectiveId { get; set; }
public string Text { get; set; }
public virtual Objective Objective { get; set; }
}
I'm currently populating the only the Objective object from this call:
var objectiveData = GetContent.GetType5();
var objectives = objectiveData.Select(o => new Objective {
Name = o.Name,
Text = o.Text}
);
The data looks like this:
Name Text
0600 header 1
0601 detail abc
0602 detail def
0603 detail ghi
0700 header 2
0701 detail xyz
Is there a way I could modify my LINQ so that only the data where the name field contents end in "00" goes into the Objective object (as it does now) and when the data where the name field contents end in "01" then it creates a new ObjectiveDetail object with "detail abc" etc going into the text field.
This is a picture of what the end result should look like:
A collection of Objectives
new Objective { name = "header 1",
ObjectiveDetails = A collection of ObjectiveDetails
name = "detail abc"
name = "detail def" etc.
Sure you can do that, using [string.EndsWith] method like:1
.Where(r=> r.Name.EndsWith("00"))
Modify your query as:
var objectives = objectiveData
.Where(r => r.Name.EndsWith("00"))
.Select(o => new Objective {
Name = o.Name,
Text = o.Text}
);
It's somewhat unclear what you are asking, but you can put complex logic inside the Select() if you need to:
var objectives = objectiveData.Select(o =>
{
var result = new Objective
{
Name = o.Name,
Text = o.Text
};
if (o.Name != null && o.Name.EndsWith("01"))
{
result.ObjectiveDetails.Add
(
new ObjectiveDetail
{
ObjectiveDetailId = o.ObjectiveId,
Name = o.Name,
Text = o.Text,
Objective = result
}
);
}
return result;
});
(Note that I'm guessing at what you need; you will need to correct the logic to do what you really want.)
Looks like you want to do some sort of conditional mapping. I like Matthew Watson's answer, but it's a bit unclear why he's always creating an Objective instance every time. Here's some LINQ-less code which I believe is more readable, and maps the way I think you'd want:
public class Mapper
{
public List<Objective> Objectives = new List<Objective>();
public class Objective
{
public int ObjectiveId { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
public Objective()
{
ObjectiveDetails = new List<ObjectiveDetail>();
}
}
public class ObjectiveDetail
{
public int ObjectiveDetailId { get; set; }
public int ObjectiveId { get; set; }
public string Text { get; set; }
public virtual Objective Objective { get; set; }
}
public void Assign()
{
var objectiveData = new[] // Hard-coded test data. We don't know what the type of each item in this list is, so I use an anonymous type
{
new {Name = "0600", Text = "Header 06"},
new {Name = "0601", Text = "06 Detail 01"},
new {Name = "0602", Text = "06 Detail 02"},
new {Name = "0603", Text = "06 Detail 03"},
new {Name = "0700", Text = "Header 07"},
new {Name = "0701", Text = "07 Detail 01"},
new {Name = "0702", Text = "07 Detail 02"}
};
// Create Objectives first
var id = 1;
foreach (var item in objectiveData.Where(i => i.Name.EndsWith("00")))
{
Objectives.Add(new Objective { ObjectiveId = id, Name = item.Name, Text = item.Text });
id++;
}
// Create ObjectiveDetails
id = 1;
foreach (var item in objectiveData.Where(i => !i.Name.EndsWith("00")))
{
var itemLocal = item;
var matchingObjective = Objectives.FirstOrDefault(o => o.Name.StartsWith(itemLocal.Name.Substring(0, 2)));
var objectiveDetail = new ObjectiveDetail
{
ObjectiveDetailId = id,
Text = item.Text,
ObjectiveId = matchingObjective != null ? matchingObjective.ObjectiveId : 0,
Objective = matchingObjective
};
if (matchingObjective != null)
{
matchingObjective.ObjectiveDetails.Add(objectiveDetail);
}
id++;
}
// At the end of this method you should have a list of Objectives, each with their ObjectiveDetails children
}
}
Output:
Hope this helps.
I have this LINQ statement that tries to set the 1st element in the collection of string[]. But it doesn't work.
Below is the LINQ statement.
docSpcItem.Where(x => x.DocID == 2146943)
.FirstOrDefault()
.FinishingOptionsDesc[0] = "new value";
public string[] FinishingOptionsDesc
{
get
{
if (this._FinishingOptionsDesc != null)
{
return (string[])this._FinishingOptionsDesc.ToArray(typeof(string));
}
return null;
}
set { this._FinishingOptionsDesc = new ArrayList(value); }
}
What's wrong with my LINQ statement above?
Couple of things.. There are some problems with your get and set. I would just use auto properties like this..
public class DocSpcItem
{
public string[] FinishingOptionsDesc { get; set; }
public int DocID { get; set; }
}
Next for your linq statement, depending on the presence of an item with an id of 2146943 you might be setting a new version of the object rather than the one you intended. This should work..
[TestMethod]
public void Linq()
{
var items = new List<DocSpcItem>();
//2146943
for (var i = 2146930; i <= 2146950; i++)
{
items.Add(new DocSpcItem()
{ DocID = i
, FinishingOptionsDesc = new string[]
{ i.ToString() }
}
);
}
var item = items.FirstOrDefault(i => i.DocID == 2146943);
if (item != null)
{
item.FinishingOptionsDesc = new string[]{"The New Value"};
}
}
and
public class DocSpcItem
{
public string[] FinishingOptionsDesc { get; set; }
public int DocID { get; set; }
}
i am new to lambda expression so i try to solve one problem .but i can't. so can anyone suggest solution for this.
i have one class customer. inside i created another 3 class and create observable collection for 3 classes.i create one observable collection for this customer
ObservableCollection<Customer> customer2;
public class Customer
{
public string CusName { get; set; }
public int CusAge { get; set; }
public ObservableCollection<Bankdetails> bankdetails;
public ObservableCollection<order> orderlist;
public ObservableCollection<orderdetails> orderdetailslist;
public class Bankdetails
{
public string Bankaccno { get; set; }
public string bankname { get; set; }
public int bankid { get; set; }
}
public class order
{
public string ordername { get; set; }
public string orderid { get; set; }
}
public class orderdetails
{
public string orderid { get; set; }
public string itemname { get; set; }
public int itemqty { get; set; }
}
}
i write one linq query for getting values from customer2.anyhow its working .like this i tried to write one lambda query but i can't.
here i adding some values to observable collection.
customer2 = new ObservableCollection<Customer>
{
new Customer()
{
CusName="nixon",CusAge=24,
bankdetails=new ObservableCollection<Customer.Bankdetails>
{
new Customer.Bankdetails()
{
bankid=12,bankname="axis",Bankaccno="09876534"
}
},
orderlist=new ObservableCollection<Customer.order>
{
new Customer.order
{
orderid="Od123",ordername="Express"
}
},
orderdetailslist=new ObservableCollection<Customer.orderdetails>
{
new Customer.orderdetails
{
orderid="Od123",itemname="cpu",itemqty=5
}
}
}
};
this is my linq query
var customer1 = from cus in customer2
from bank in cus.bankdetails
from ord in cus.orderlist
from orddet in cus.orderdetailslist
where ord.orderid == orddet.orderid
select new
{
cus.CusAge,cus.CusName,
bank.Bankaccno,bank.bankid,bank.bankname,
ord.ordername,
orddet.itemname,orddet.itemqty
};
then what will be the lambda query.pls anyone suggest .
Matt's solution extended with the where from the question would be:
var xxx = customer2.SelectMany(cus =>
cus.bankdetails.SelectMany(bank =>
cus.orderlist.SelectMany(ord =>
cus.orderdetailslist.Where(orddet => orddet.orderid == ord.orderid)
.Select(orddet => new
{
cus.CusAge,
cus.CusName,
bank.Bankaccno,
bank.bankname,
orddet.itemname,
orddet.itemqty
}
)
)
)
);
var xxx = customer2.SelectMany(cus =>
cus.bankdetails.SelectMany(bank =>
cus.orderlist.SelectMany(ord =>
cus.orderdetailslist.Select(orddet => new
{
cus.CusAge,
cus.CusName,
bank.Bankaccno,
bank.bankname,
orddet.itemname,
orddet.itemqty
}
)
)
)
);
I have a list of items, i.e, List<SearchFilter>, and this is the SearchFilter object:
public class SearchFilter
{
public int ItemID { get { return ValueInt("ItemID"); } }
public string ItemName { get { return ValueString("ItemName"); } }
public string Type { get { return ValueString("Type"); } }
}
How do I group by the Type, and project the grouped item into a new list of GroupedFilter, i.e:
public class Filter
{
public int ItemID { get; set; }
public string ItemName { get; set; }
}
public class GroupedFilter
{
public int Type { get; set; }
public List<Filter> Filters { get; set; }
}
Thanks.
var result = items.GroupBy(
sf => sf.Type,
sf => new Filter() { ItemID = sf.ItemID, ItemName = sf.ItemName },
(t, f) => new GroupedFilter() { Type = t, Filters = new List<Filter>(f) });
But you need to make sure your GroupedFilter.Type property is a string to match your SearchFilter.Type property.
With Linq query syntax it is longer and more complex but just for reference:
var grpFilters = (from itm in list group itm by itm.Type into grp select
new GroupedFilter
{
Type = grp.Key,
Filters = grp.Select(g => new Filter
{
ItemID = g.ItemID,
ItemName = g.ItemName
}).ToList()
}).ToList();
Somebody may find it more readable because they don't know all the possible parameters to GroupBy().