數據庫字段中的逗號分隔會傷害我的感受。考慮一個位掩碼:
[Flags]
public enum DayOfWeek
{
Undefined = 0,
Mon = 1,
Tue = 2,
Wed = 4,
Thu = 8,
Fri = 16,
Sat = 32,
Sun = 64
}
DayOfWeek bitmask = DayOfWeek.Mon | DayOfWeek.Wed | DayOfWeek.Sat;
這樣你就保持了理智和查詢能力。
在SQL查詢此:
DECLARE @bitmast INT;
SET @bitmask = 64 | 1 | 16; -- sun, mon, fri
SELECT * FROM tbl
WHERE DayOfWeekBitMask & @bitmask = @bitmask;
要使用LINQ查詢這對實體:
int bitmask = DayOfWeek.Sun | DayOfWeek.Mon |DayOfWeek.Fri;
var query = Context.tbl.Where(r => (r.DayOfWeekBitMask & bitmask) == bitmask);
要堅持:
int bitmask = DayOfWeek.Sun | DayOfWeek.Mon |DayOfWeek.Fri;
var item = Context.tbl.First();
item.DayOfWeekBitMask = bitmask;
Context.SaveChanges();
有趣,我也想過比特種類面具和一個XML字段...你如何存儲和查詢星期日,星期一和星期五從DayOfWeek,這將是非常有益的,以獲得整個我數據包絡分析! – Elisabeth 2013-03-21 20:40:31
@Elisa我已經添加了更新的示例,並選擇使用實體並使用SQL進行選擇。 – 2013-03-21 20:45:57
感謝弗萊姆非常好我也upvoted你的解決方案:) – Elisabeth 2013-03-21 20:53:35