简体   繁体   中英

LINQ to SQL using C#

In a C# 2008 windows application, I have the following linq to sql statement that points to a sql server 2008 r2 database. The following query returns null. But there are records to be fetched.

//Code

   string SubmissionPkgID = "valid string value";
   var varGoodTransCount = (from t in rData.Transactions
   join iw in rData.Ibooks on t.ImportID equals iw.ImportID
   join ip in rData.IPackages on iw.PID equals ip.PID
   where (ip.trc_num != null) && ip.trc_num == SubmissionPkgID
   group ip by ip.trc_num into g
   select new { trc_num = g.Key, Frequency = g.Count() }).FirstOrDefault();

Note: SubmissionPkgID does contain a valid value. Where i'm worng?

Your linq query look ok it must work. rData.IPackages.trc_num is int or mumeric right? so you may use HasValue instead of checking Null value.

//Code

string SubmissionPkgID = "valid string value";
var varGoodTransCount = (from t in rData.Transactions
                        join iw in rData.Ibooks on t.ImportID equals iw.ImportID
                        join ip in rData.IPackages on iw.PID equals ip.PID
                        where (ip.trc_num.HasValue)  && ip.trc_num == SubmissionPkgID
                        group ip by ip.trc_num into g
                        select new { trc_num = g.Key, Frequency = g.Count() })
   .FirstOrDefault();

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM