c# - How do I rewrite this SQL query in LINQ format? -


I have a SQL query that returns the surrounding rows for a given ID. So we say that I'm trying to find 3 rows for a given media id of 8000. (Previous row, current row, next line).

I am also not sure if this is the best way to get my results here on any of those results:

  SELECT * (SELECT TOP 1 * Media where MediaIDype = 1 and MediaID & lt; 8000 Order Media Select ID DESC Union * MediaID = 8000 UNION SELECT TOP 1 * Media where mediaTipid = 1 and Media ID> 8000) TBL by order of TBL. Media ID  

I am importing this query into a C # web application and would like to convert the query to LINQ format. I am struggling with this. I think IM is off .

Here is my LINQ code:

  // Previous record, get current record, next record var result = (in DB from M. Media where m.mediaiatid = = 1 & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; एमडीआई & lt; 8000 orderbay M. media descending selection M). Take (1). Union (from DB Media in M, where M. mediaid == 8000m choose). From Union D.B. Media, from M, where M. MediaTapID == 1 & amp; Amp; M. MediDide & gt; Choose 8000 m). (1) Take;  

Thank you for your help.

It looks very close. I think that you need an extra set of brackets around your final statement of "union" (so that take (1) applies only to that final statement and until that point the whole federal Does not apply to the LINQ statement) and a final order b :

  var result = (in DB media where M. Media typed == 1 & Amp; amp; m. Media id & lt; 8000 orderbay M. mediad descending selection m). Lake (1). Union (from DB Media in M. where M. MediaID == 8000 Choose Me) Choose from M.ImiaTypeid == 1 & amp;; Amp; M. Media ID & gt; 8000 in the meter from the Union (DB.MD). Take (1)). Orderbie (m => m. Mediad);  

For clarity, consider dismissing it in separate lines:

var lessThan8000 = (In DB the media where m.MediaTypeID == 1 & amp; Amp; amp; M. Media ID & lt; 8000 orderbay M. Medial descending selection M). Take (1); Var equalTo8000 = (DB in Media where M. MediaAd == 8000m); Var greaterThan8000 = (DB. Media Select from M. MediaTapID == 1 & amp; amp; amp; M.MediaID> 8000m). Take (1)); Var result = less tan 8000. Union (equal TOO 8000). Union (mostly 8000). Orderbie (m => m. Media id);

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -