Linq to sql 中sql null 空字符串串为null时该怎么写

linq to sql中怎样执行tsql_百度知道
linq to sql中怎样执行tsql
我执行的查询有点复杂,都是要根据条件拼接的sql我想在linq中执行sql代码怎样才行呢?
我有更好的答案
LINQ数据库实体对象.ExecuteCommand(“T-SQLTEXT”,paras[]);
表示 LINQ to SQL 框架的主入口点。
public class DataContext : IDisposable{
直接对数据库执行 SQL 命令。
要执行的 SQL 命令。
parameters:
要传递给命令的参数数组。注意下面的行为:如果数组中的对象的数目小于命令字符串中已标识的最大数,则会引发异常。如果数组包含未在命令字符串中引用的对象,则不会引发异常。如果任一参数为
null,则该参数会转换为 DBNull.Value。
// 返回结果:
一个 int,表示所执行命令修改...
LINQ技术其实是先将数据库导入创建了一个数据集~~然后从数据集中直接查询你所需要的数据的·~~语句写法跟TSQL语句没多大区别·~只是看着更直观·~在程序执行过程中获取数据的速度更快·~个人理解·~
举个例子吧:根据条件查询书本:
其他类似问题
为您推荐:
linq的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁linq to sql 不确定查询变量是否为空,应该怎么写_百度知道
linq to sql 不确定查询变量是否为空,应该怎么写
输入的若干变量作为查询条件,但是不确定哪一个textbox会背用户输入,这种查询语句怎么写啊我有一个界面里面有若干textbox输入
提问者采纳
x==textbox1.y==textbox2.IsNullOrEmpty(textbox1.Text,里面有x列和y列var a=from b in list where b.IsNullOrEmpty(textbox1,表名为list,有个表假设有两个Textbox.Text.Text.Trim()||string.Trim()) &&(b,分别为textbox1和textbox2.Trim()))
select b.Trim()||string.Text
提问者评价
太感谢了,真心有用
来自:求助得到的回答
其他类似问题
为您推荐:
linq的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁c# - LINQ to SQL sum null value - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I have the following query, I'd like to sum the NULL value also. Some TimeSheet don't records in TimeRecord and some tr.TimeIn and tr.TimeOut are NULL.
The query select only TimeSheet that has reords in TimeRecord. How I can have it select everything, and sum up the NULL value as well. So, the SUM of NULL will be just zero.
Table relationship:
Student 1:N TimeSheet (FK StudentId)
TimeSheet 1:N TimeRecord (FK TimeSheetId)
TimeIn and TimeOut are DateTime type and nullable.
Query 1: Monthy Report:
Dim query = From ts In db.TimeSheets _
Join tr In db.TimeRecords On tr.TimeSheetId Equals ts.TimeSheetId _
Where ts.IsArchive = False And ts.IsCompleted = False And tr.TimeOut IsNot Nothing _
Group By key = New With {ts.Student, .MonthYear = (tr.TimeOut.Value.Month & "/" & tr.TimeOut.Value.Year)} Into TotalHour = Sum(DateDiffSecond(tr.TimeIn, tr.TimeOut)) _
Select key.Student.StudentId, key.Student.AssignedId, key.MonthYear, TotalHour
Query 2: Total TimeRecord for Student with Active TimeSheet:
Dim query = From ts In db.TimeSheets _
Join tr In db.TimeRecords On tr.TimeSheetId Equals ts.TimeSheetId _
Where ts.IsArchive = False And ts.IsCompleted = False _
Group By ts.StudentId, tr.TimeSheetId Into TotalTime = Sum(DateDiffSecond(tr.TimeIn, tr.TimeOut)) _
Select StudentId, TimeSheetId, TotalTime
Here's the result of the query 2:
734 -- 159 : 9 hrs 35 mm 28 sec
2655 -- 160 : 93 hrs 33 mm 50 sec
1566 -- 161 : 37 hrs 23 mm 53 sec
3114 -- 162 : 25 hrs 0 mm 21 sec
Wanted result of Query 2:
733 -- 158 : 0 hr 0mm 0 sec
734 -- 159 : 9 hrs 35 mm 28 sec
736 -- 169 : 0 hrs 0mm 0sec
2655 -- 160 : 93 hrs 33 mm 50 sec
1566 -- 161 : 37 hrs 23 mm 53 sec
3114 -- 162 : 25 hrs 0 mm 21 sec
Same for Query 1 but it makes monthly report.
38k16105194
I apologise because I translated your query to C# before tweaking it, and I don’t really know the VB syntax well enough to translate it back, but I hope that you will be able to. I tried the following query and it does what you asked for:
var query = from st in Students
select new
st.StudentId,
st.AssignedId,
TotalHour = (
from ts in TimeSheets
where ts.StudentId == st.StudentId
join tr in TimeRecords on ts.TimeSheetId equals tr.TimeSheetId
where !ts.IsArchive && !ts.IsCompleted && tr.TimeOut != null
select (tr.TimeOut.Value - tr.TimeIn).TotalHours
I had to remove the MonthYear thing because I didn’t really understand how that fit in with your grouping, but since it’s not in the output, I suspected that maybe you don’t need it.
I had to make a few assumptions:
I am assuming that TimeOut is a DateTime? (nullable) while TimeIn is DateTime (non-nullable). I think that makes sense.
I am assuming that TimeSheets have a StudentId that links them to students.
38k16105194
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 linq怎么写 的文章

 

随机推荐