我在db表中有以下内容:
- MailingId | GroupName | ServiceId
- 1 | group1 | 3
- 2 | group1 | 5
- 3 | group1 | 8
- 4 | group2 | null
- 5 | group3 | null
...
在我看来,我有两组复选框:
我需要使用LINQ选择以下内容:
选择我在ServiceId中选择的行复选框列表PLUS中的任何其他行。例如,如果我检查ServiceId(3和5)和组“Group3”,那么我的输出将是行MailingId:1,3和5.但是,如果我从(第一组复选框)中选择任何服务并且不要选择“ Group1“来自邮件组复选框,然后Group1的行不应该在输出中。
我正在使用EF4。请帮忙。 感谢
答案 0 :(得分:2)
2个数组将是从您的视图中发布的选择
int[] selectedservices = {3,5};
string[] selectedgroups = {"group3"};
using (Model model = new Model())
{
bool b = selectedservices.Contains(1);
var mailinglists = from m in model.MalingSet
where selectedgroups.Contains(m.GroupName)
&& ((m.ServiceId.HasValue && selectedservices.Contains(m.ServiceId.Value)) || m.ServiceId.HasValue == false)
select m.MailingId;
}
答案 1 :(得分:0)
尝试这样的事情。
var mailingGroup =
from m in Mailings
where m.ServiceId != null // or whatever other condition
group m by m.GroupName into g
select new
{
groupName = g.Key,
mailings = g
};
答案 2 :(得分:0)
这是Where子句否?
var a = new {m = 1, g = "group1", sid = 3};
var b = new {m = 2, g = "group1", sid = 5};
var c = new {m = 3, g = "group1", sid = 8};
var d = new {m = 4, g = "group2", sid = 0};
var e = new {m = 5, g = "group3", sid = 0};
var l = new List<dynamic>{a,b,c,d,e};
l.Where(it => ( new ArrayList{3, 5}).Contains(it.sid)
|| (new ArrayList{1, 5}).Contains(it.m)).Dump();