使用linq&获得基于两列的类似数据的计数asp.net [英] get count of similar data based on two columns using linq & asp.net

查看:87
本文介绍了使用linq&获得基于两列的类似数据的计数asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表包含在列和日期之后。

I have a DB table consists following Columns and Date.




表1

id | CauseID(forgeinkey)| Datefrom |标题

----------------------------------------- -

1 | 2 | 09/10/2014 | title1

2 | 1 | 09/20/2014 | title2

3 | 1 | 09/20/2014 | title3

4 | 3 | 09/30/2014 | title4

5 | 3 | 09/30/2014 | title5





表2

CauseID |标题

--------------------

1 |因为1

2 | cause2

3 | cause3



如果用户输入09月,我想要的是显示上表中的数据应该是以下格式:



cause1(2)-title2-title3

(因为原因1在2014年9月20日的表中发生了两次)



cause3(2)-title4-title5

(因为在2014年9月30日,表格中有两次出现)



cause2 (1)-title1

(因为原因2在2014年9月10日的表中发生过一次)



有人可以告诉我怎么样在linq asp.net中做到这一点

非常感谢你提前


Table1
id | CauseID(forgeinkey) | Datefrom | Title
-------------------------------------------
1 | 2 |09/10/2014 | title1
2 | 1 |09/20/2014 | title2
3 | 1 |09/20/2014 | title3
4 | 3 |09/30/2014 | title4
5 | 3 |09/30/2014 | title5


Table2
CauseID | Title
--------------------
1 | cause1
2 | cause2
3 | cause3

What i want that if user enters month 09 show the data from above table should be get in following format:

cause1(2)-title2-title3
(beacuse cause1 occures two times in Table in 09/20/2014)

cause3(2)-title4-title5
(beacuse cause3 occures two times in Table in 09/30/2014)

cause2(1)-title1
(beacuse cause2 occures one time in Table in 09/10/2014)

can somebody please tell me how to do it in linq asp.net
thank you very very much in advance

推荐答案

在txt1中你可以输入一个像9 /的日期20/2014

DateTime dt = Convert.DateTime(txt1.Text);

var query =来自上下文中的T1.Table1

加入T2在context.Table2 on T!.CauseID等于T2.CauseID

其中SqiFunctions.DateDiff(DAY,T1.Dateform,dt)== 0

选择新

{

T2.Title,

T1.Title

}





您将获得两条记录,您需要进行简单格式化才能获得上述格式的结果
In txt1 you can enter a date like 9/20/2014
DateTime dt = Convert.DateTime(txt1.Text);
var query = from T1 in context.Table1
join T2 in context.Table2 on T!.CauseID equals T2.CauseID
where SqiFunctions.DateDiff("DAY",T1.Dateform,dt) == 0
select new
{
T2.Title,
T1.Title
}


You will get two records and you need to do simple formatting in order to get result in above format


这篇关于使用linq&获得基于两列的类似数据的计数asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆