I have a table where it has the columns like IsAgreed, IsOther, IsEquipped. On the UI I am showing 3 check boxes where one can select single or multiple check boxes. The data is getting saved to db as expected. Now I am trying to select the data in entity framework as follows
from tbl context.TableNames select new {
Conitions= tbl.IsOther ? tbl.OtherText : tbl.IsAgreed ? "Agreed :
tbl.IsEquipped? "Equipped" : "" }
Which is giving only one selection when the multiple selection are made. I would like to concat and result the data so that it can be
OtherText, Agreed, Equipped
OtherText, Equipped
Agreed, Equipped
Is it possible to concatenate and give the expected output
CodePudding user response:
You can create an array of strings based on conditions, after it can be formatted as desired. Please pay attention to the comment i wrote in the code sample.
var conditions = context.TableNames.Select(tbl => new
{
tbl.IsOther,
tbl.IsAgreed,
tbl.IsEquipped
})
.AsEnumerable() //Should be used with caution. Because it will load each record to memory. It also switches "LINQ to Entities" to "LINQ to Objects", so we can use string.Join.
.Select(c => new
{
Conditions = string.Join(", ", new string[] { c.IsOther ? "OtherText" : "", c.IsAgreed ? "Agreed" : "", c.IsEquipped ? "Equipped" : "" }.Where(s => !string.IsNullOrEmpty(s)))
});