If you Know baics of Linq to sql then you apply this directly
YourDataContext ctx = new YourDataContext ();
var abc=ctx.TableName.Where(f => f.ColumnName.StartsWith(prefixText)).Select();
or
var abc=ctx.TableName.Where(f => f.ColumnName.Contains(prefixText)).Select();
Kamiran
Member
225 Points
196 Posts
This query in Linq to Sql
Mar 02, 2012 04:03 AM|LINK
Hello all..
I have this SQL query and wanted to know how can i write it in linq to sql.?
thanks in advance.
me_ritz
Star
9339 Points
1448 Posts
Re: This query in Linq to Sql
Mar 02, 2012 04:08 AM|LINK
Hope these links help you..
http://stackoverflow.com/questions/835790/how-to-do-sql-like-in-linq
http://www.simonrhart.com/2008/06/using-like-in-linq-to-sql-under-c.html
daisydain
Member
222 Points
51 Posts
Re: This query in Linq to Sql
Mar 02, 2012 04:15 AM|LINK
Hello,
Try the link below. It will help you
http://www.simonrhart.com/2008/06/using-like-in-linq-to-sql-under-c.html
vish02chouha...
Member
303 Points
253 Posts
Re: This query in Linq to Sql
Mar 02, 2012 04:22 AM|LINK
If you Know baics of Linq to sql then you apply this directly
YourDataContext ctx = new YourDataContext (); var abc=ctx.TableName.Where(f => f.ColumnName.StartsWith(prefixText)).Select(); or var abc=ctx.TableName.Where(f => f.ColumnName.Contains(prefixText)).Select();other wise use the links suggested above
arunabathan
Member
730 Points
236 Posts
Re: This query in Linq to Sql
Mar 02, 2012 04:29 AM|LINK
hi,
Please refer below url,
http://www.codeproject.com/Articles/26657/Simple-LINQ-to-SQL-in-C
http://msdn.microsoft.com/en-us/library/bb425822.aspx
http://codesamplez.com/database/linq-to-sql-c-sharp-tutorial
Thanks&Regards,
Arunabathan.G
Kamiran
Member
225 Points
196 Posts
Re: This query in Linq to Sql
Mar 02, 2012 04:34 AM|LINK
Hello me_ritz.
thanks for your reply. this is fine. Contains() works for "%text%".
what about this: i have a method where i check a seatnumber, i get the number as a parameter
private string checkSeatValue(int busTripId, int seatNumber) { string sNumber = "s" + seatNumber.ToString(); ... some code here... string sqlCommand = "SELECT " + sNumber + " FROM TBus WHERE Id=" + busTripId + ""; ... more code here... }me_ritz
Star
9339 Points
1448 Posts
Re: This query in Linq to Sql
Mar 02, 2012 05:24 AM|LINK
More Info:
http://msdn.microsoft.com/en-us/vstudio/bb688085.aspx
Kamiran
Member
225 Points
196 Posts
Re: This query in Linq to Sql
Mar 02, 2012 06:03 AM|LINK
Thank you all for your replay, your answers are helpfull, but i still miss this one.
private string checkSeatValue(int busTripId, int seatNumber) { string sNumber = "s" + seatNumber.ToString(); ... some code here... string sqlCommand = "SELECT " + sNumber + " FROM TBus WHERE Id=" + busTripId + ""; ... more code here... }i have in my database table TBus fields like s1,s2.s3.s4,...snvish02chouha...
Member
303 Points
253 Posts
Re: This query in Linq to Sql
Mar 02, 2012 10:57 AM|LINK
For this you have to include Dynamic.cs File in your project which you get from this site
NorthwindDataContext northwind = new NorthwindDataContext(); string sqlCommand = "SELECT " + sNumber + " FROM TBus WHERE Id=" + busTripId + ""; string sNumber = "s" + seatNumber.ToString(); var query = northwind.YourTableName .Where("id = " + sNumber + "") .select(sNumber);Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: This query in Linq to Sql
Mar 04, 2012 12:27 AM|LINK
Hello Kamiran:)
In linq-to-sql,you can use "Contains" instead——
var result = from e in XXX.TableEntityModels where e.Field.Contains(your value here) select e;