sql-to-linq-conversion

Convert SQL with multiple join into LINQ

半城伤御伤魂 提交于 2020-01-04 14:23:47
问题 I would like to know how can i change the following sql statement into Linq or Lambda Extension in C# SELECT m.mdes as AgeGroup,COUNT(DISTINCT(mbcd))as "No.of Member" FROM mageg m LEFT JOIN (select distinct(mbcd) ,mage FROMtevtl JOIN mvipm ON tevtl.mbcd = mvipm.mvip WHERE datm >= '2014-04-01' AND datm <= '2014-04-30' )vip ON m.tage >= vip.mage AND m.fage <= vip.mage GROUP BY m.mdes I manage to do the first half of the LINQ statement. Not sure If it is correct here is the first half. I do not

Checking for Nulls on DB Record Mapping

此生再无相见时 提交于 2020-01-01 17:58:20
问题 How can I check for db null values in the attached code? Please understand I am a new C# convert... What this code does is takes a IDataReader object and converts and maps it to a strongly-typed list of objects. But what I am finding is it completely errors out when there are null columns returned in the reader. Converter internal class Converter<T> where T : new() { // Declare our _converter delegate readonly Func<IDataReader, T> _converter; // Declare our internal dataReader readonly

Using LINQ find nearby places from database

落爺英雄遲暮 提交于 2020-01-01 10:28:08
问题 We want to receive list of nearby places from database using LINQ in ASP.NET 2012 and would like some feedback on our strategy. My table and fake data: PlaceId Name Latitude Longitude 1 A 18.1 20.1 2 B 18.2 20.2 3 C 18.3 20.3 1) In our project the client current location (latitude and longitude) is taken as input 2) At server side ,depending upon the client current location, we need to find nearby places from the database using LINQ Here's the code for SQL which I earlier used , but now we

Convert SQL query into LINQ query in VB

匆匆过客 提交于 2019-12-25 01:38:22
问题 I would like to know how to convert the SQL query listed below into a LINQ query in VB: SELECT * FROM (SELECT SUM(t.Amt) Tax, g.GenQuoteDetails_Id, g.lastTkdt, g.QuoteDt, g.BaseFareCurrency, g.BaseFareAmt, g.BaseDecPos, g.LowestorNUCFare, g.EquivCurrency, g.EquivAmt, g.EquivDecPos,g.TotCurrency,g.TotAmt,g.TotDecPos,fareinfo_id FROM GenQuoteDetails g INNER JOIN TaxData t ON t.TaxDataAry_Id = g.GenQuoteDetails_Id GROUP BY g.GenQuoteDetails_Id, g.lastTkdt, g.QuoteDt, g.BaseFareCurrency, g

SQL to LINQ conversion with NOT IN

匆匆过客 提交于 2019-12-23 05:17:29
问题 please form a LINQ query for the mysql query select a.name ,a.amount from acount as a where a.acountid NOT IN (select c.id from saving as c where c.userid="x") and a.userid="x"; X=1; Please help me out Thanks 回答1: Have not tested at all but something in these lines should work... var query = from a in db.Account where !(from s in db.Savings where s.UserId == "x" select s.id) .Contains(a.AccountId) && a.UserId == "x" select new { a.Name, a.Amount }; 来源: https://stackoverflow.com/questions

How to do left joins in LINQ on multiple fields in single join

一个人想着一个人 提交于 2019-12-22 04:37:27
问题 I am trying to do this simple sql query to LINQ. But its give me error. Here is the SQL query that need to conver to LINQ DECLARE @groupID int SET @groupID = 2 SELECT * FROM dbo.Person p LEFT JOIN dbo.PersonGroup pg ON ( p.PersonID = pg.PersonID AND pg.GroupID = @groupID) Ignore @groupID. which will be provided as function parameter for LINQ query. Here is LINQ query what i have tried. from p in Person join pg in PersonGroup on new { p.PersonID, groupID } equals new { pg.PersonID, pg.GroupID

Convert SQL into Linq query

安稳与你 提交于 2019-12-20 02:16:07
问题 I'm quite new at Linq queries, I just want to convert my DB query into Linq. Here is my simple SQL query: var query = "SELECT EnrollmentDate, COUNT(*) AS StudentCount " + "FROM Person " + "WHERE EnrollmentDate IS NOT NULL " + "GROUP BY EnrollmentDate"; var data = db.Database.SqlQuery<EnrollmentDateGroup>(query); It is working fine , but how could it possible to write this query in Linq , I just can't convert the group by statement into Linq. It seems somewhat tricky to convert into Linq. Can

How do I convert this SQL inner join query into LINQ syntax?

旧时模样 提交于 2019-12-12 05:14:56
问题 I'm not that familiar with LINQ. I need this query converted into a LINQ statement for use inside my C# project. Thanks SELECT Galleries.GalleryTitle, Media.* FROM Galleries INNER JOIN Media ON Galleries.GalleryID = Media.GalleryID WHERE (Galleries.GalleryID = 100) 回答1: var query = from g in db.Galleries join m in db.Media on g.GalleryID equals m.GalleryID into gm where g.GalleryID == 100 select new { g.GalleryTitle, Media = gm }; Property Media will contain list of joined media entities.

Linq To SQL - Having and Group By

巧了我就是萌 提交于 2019-12-11 02:43:48
问题 I've this query below working fine. However I want to implement it using Linq. select u.ID, u.NAME from Task t join BuildingUser bu ON bu.ID_BUILDING = t.ID_BUILDING join [User] u ON u.ID = bu.ID_USER where t.ID IN (2,9) AND u.ID != t.ID_USER group by u.id, u.name having count(bu.ID_BUILDING) = (SELECT COUNT(t2.ID_BUILDING) FROM Task t2 WHERE t2.ID IN (2,9)) I don't know how to Group and use Having clause at the same time. 回答1: You can try something like this: var ids = new[] { 2, 9 }; var

SQL LIKE in Linq

你说的曾经没有我的故事 提交于 2019-12-10 13:47:44
问题 Before adding this question, I did search on stackoverflow for similar ones but I couldnt find. Most of the questions over internet were using LIKE with a string (for eg LIKE '%ABC%') but I need to compare with an existing column of a different table. I need to write a linq query for the select statement as below - select * from [dbo].[BaseClaim] where WPId like (select WPId from UserProfiles where ID='1459') I came up with below linq query but its not working as expected - var result = (from