我有一個表,其中包含 IsAgreed、IsOther、IsEquipped 等列。在 UI 上,我顯示了 3 個復選框,您可以在其中選擇單個或多個復選框。資料正按預期保存到資料庫中。現在我正在嘗試按如下方式選擇物體框架中的資料
from tbl context.TableNames select new {
Conitions= tbl.IsOther ? tbl.OtherText : tbl.IsAgreed ? "Agreed :
tbl.IsEquipped? "Equipped" : "" }
當進行多項選擇時,它只給出一個選擇。我想連接并生成資料,以便它可以
OtherText, Agreed, Equipped
OtherText, Equipped
Agreed, Equipped
是否可以連接并給出預期的輸出
uj5u.com熱心網友回復:
您可以根據條件創建一個字串陣列,然后根據需要對其進行格式化。請注意我在代碼示例中寫的注釋。
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)))
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359186.html
上一篇:將列舉引數與c#運算式一起使用
