linq 多条件组合查询

longyumen 2011-01-17 12:45:06
tb_user
user_id
user_name
user_birth //用户生日
tb_eng
eng_id
user_id
create_time //创建日期

根据user_id连接以上两个表 查询条件是 用户的年龄大于18小于25 && 创建日期+32天小于当前日期 用LINQ怎么写? 似乎LINQ中不支持AddDays()之类的函数

var query=from s in dc.students where GetAge(s.birth)>18 &&GetAge(s.birth)<25
select s;

int GetAge(string birthday)
{
try
{
return DateTime.Now.Year-Convert.ToDateTime(birthday).Year;
}
catch {return 0;}
}
不考虑表连接的的话 以上这种方法似乎也不行

哪位高手帮忙解决下 我是在做个搜索功能
...全文
447 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
longyumen 2011-01-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 q107770540 的回复:]
try:


C# code

var query=(from s in dc.students
select s).ToList();

query=query.Where(s=>GetAge(s.birth)>18 && GetAge(s.birth)<25);

int GetAge(string birthday)
{
try
{
r……
[/Quote]

先toList() 是会影响查询效率的吧? 能避免么?
宝_爸 2011-01-17
  • 打赏
  • 举报
回复
你的方法看起来应该可以啊
宝_爸 2011-01-17
  • 打赏
  • 举报
回复
birth没有映射成DateTime类型吗?
longyumen 2011-01-17
  • 打赏
  • 举报
回复
用的是LINQ to Entities
longyumen 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 q107770540 的回复:]
try:


C# code

var query=(from s in dc.students
select s).ToList();

query=query.Where(s=>GetAge(s.birth)>18 && GetAge(s.birth)<25);

int GetAge(string birthday)
{
try
{
r……
[/Quote]

通过 非常感谢 我在调试下
q107770540 2011-01-17
  • 打赏
  • 举报
回复
try:


var query=(from s in dc.students
select s).ToList();

query=query.Where(s=>GetAge(s.birth)>18 && GetAge(s.birth)<25);

int GetAge(string birthday)
{
try
{
return DateTime.Now.Year-Convert.ToDateTime(birthday).Year;
}
catch {return 0;}
}

http://msdn.microsoft.com/en-us/library/bb738550.aspx
longyumen 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 q107770540 的回复:]
引用楼主 longyumen 的回复:
不考虑表连接的的话 以上这种方法似乎也不行


具体不行 报错是什么?
[/Quote]

{"LINQ to Entities 不识别方法“Int32 GetAge(System.DateTime)”,因此该方法无法转换为存储表达式。"} System.SystemException {System.NotSupportedException}


就是这个错误
rczjp 2011-01-17
  • 打赏
  • 举报
回复
是不是你表里面有NULL的用户生日
今年-生日年就是多大岁数,就是一个数字怎么会错?
q107770540 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 longyumen 的回复:]
不考虑表连接的的话 以上这种方法似乎也不行
[/Quote]

具体不行 报错是什么?
longyumen 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 findcaiyzh 的回复:]
birth没有映射成DateTime类型吗?
[/Quote]

就是DateTime类型的
宝_爸 2011-01-17
  • 打赏
  • 举报
回复
测了下你的代码,没问题啊


class User
{
public int user_id;
public string user_name;
public string user_birth;
}


private void Form1_Load(object sender, EventArgs e)
{
List<User> users = new List<User>{
new User{user_id = 0, user_name = "000", user_birth = new DateTime(1978,2,3).ToString()},
new User{user_id = 1, user_name = "111", user_birth = new DateTime(1988,2,3).ToString()},
new User{user_id = 2, user_name = "222", user_birth = new DateTime(2008,2,3).ToString()}
};

var query = from s in users
where GetAge(s.user_birth) > 18 && GetAge(s.user_birth) < 25
select s;

foreach (User u in query)
{
Console.WriteLine(u.user_name + " " + u.user_birth);
}
}

int GetAge(string birthday)
{
try
{
int age = DateTime.Now.Year - Convert.ToDateTime(birthday).Year;
return age;
}
catch { return 0; }
}


8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧