.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

select certian records linq to sql

Posted By:      Posted Date: October 10, 2010    Points: 0   Category :ASP.Net

I have a database with a GUID and images,

one function

I want to get the first record (if exists) and return results


 second function

i want to get the second record only if a second record exists


I guess I have to return the records frmo the table then just movenext so to speak in a loop to select the second record but guessing there is a better faster way to do that in linq

I see some example for .take(1)  I assume that will always take the first record and return nothing else. I dont need this to be random. and what happens if there are no records return?

for the second function

I gues its  .skip(1).take(1)   will that for sure skip the first record and return the  second one to my var? again what if thier is no second record i would not want any results return if this is not the case (no scond record then want nothing back and no error)



View Complete Post

More Related Resource Links

select records within 30 days of previous records

I have a table with a list of patient id's and there begin date and end dates as follows: ID     begin_date  end_date 1234 10/09/2009 10/16/2009 1234 12/16/2009 12/22/2009 1234 12/28/2009 12/30/2009 1234 01/13/2010 01/19/2010 1234 04/29/2010 05/05/2010 what i need to do is select the records that have an end_date within 30 days of a begin_date. So in the above example, i would need to get the following records: ID     begin_date   end_date 1234 12/28/2009   12/30/2009 1234 01/13/2010   01/19/2010 Because 12/28/2009 is 30 days within 12/22/2009 (previous end_date) and 01/13/2010 is within 30 days of 12/30/2009 (previous end_date) and 12/22/2009 (2nd previous end_date). is there a good way to do this using T-SQL?

Problem updating records with LINQ, Concurrency?

 My project includes a grid view with some updtable fields, some fields throw an error on update and some do not and it does not really make sense. WORK_STATION_ID does not cause an error, ROOM _ID cause an error, both are int? (I am using c#) and for update I use the code liste below.using (DataClassesDataContext db = new DataClassesDataContext()) { var product = (from p in db.ASSETs where p.ASSET_ID == ASSET_ID select p).Single(); product.WORK_STATION_ID=WORK_STATION_ID; product.ROOM_ID=ROOM_ID; db.SubmitChanges(); }    Row not found or changed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.Linq.ChangeConflictException: Row not found or changed. Source Error: Line 93: Line 94: Line 95: db.SubmitChanges(); Line 96: Line 97: Source File: C:\Projects\TEST_II\trunk\ObjectDataSourceLibrary\AssetController.cs Line: 95 Stack Trace: [ChangeConflictException: Row not found or changed.] System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +963417 Syst

Select random 10 records in sql server 2005 in terms of time

Hello,i have a query that select random records in access database in terms of time : "SELECT TOP 10 * FROM Tbl ORDER BY Rnd(-10000000*TimeValue(Now())*[ID])"now i wish to convert this query for using in sql server 2005 such that return random records in term of time 

Use of PATINDEX function in select clause With Linq

hello       i have an problem to use of PATINDEX function in select clause with Linq.      if anyone have any clue about it then please update me its urgent.Thanks In Advance.

Insert a "Select User" row in Linq for a dropdown list

I'm new to Linq. I have searched and searched the web for a soluion, and can't find anything. I have a Linq query and I want to insert a row ("Select User") to the top before I pass it to the drop down list. I've been trying to use the Union but to now avail (it keeps telling me that my object doesn't support the Union method). My code, prior to attempting to inser a row, is very simple.public SelectList DropDown_Users()        {            var context = new VivarianDataContext();            var query = from t in context.AspnetUsers                        select new { t.UserId, t.LastName };            list = new SelectList(query.AsEnumerable(), "UserId", "LastName");            return list;        }Now I try to insert a row and I found this on the internet and it seems to say that his solution will work. But it is filled with syntax errors. http://magicode.wordpress.com/2009/08/20/inserting-an-item-in-iqueryable-object-using-union-met

Is the delete records LINQ correct ?

I hope to delete all records in DBPrograms, is the following code correct?               DBContext db = DBContext(PublicDBPar.ConnectionString);            db.DBPrograms.DeleteAllOnSubmit(db.DBPrograms);            db.SubmitChanges();

LINQ. select query with OR.

Executing 'SELECT' query with 'WHERE' clause which have 'OR' and 'AND' operators give wrong result. Example: Have 4 tables: table1,table21,table22,table3. Relations between it: 'table1' OneToMany 'table21' and 'table22',  'table3' OneToMany 'table21' and 'table22'. tables 'table21' and 'table22' store relations between 'table1' and 'table3'. Example query { from t1 in table1 from t21 in table21 from t22 in table22 from t3 in table3 where (t1.ID==t21.t1ID & t21.t3ID==t3.ID) | (t1.ID==t22.t1ID & t22.t3ID==t3.ID) select t3; } give huge amount duplicate results Although similar query { ( from t1 in table1 from t21 in table21 from t3 in table3 where (t1.ID==t21.t1ID & t21.t3ID==t3.ID) select t3 ).Union( from t1 in table1 from t22 in table22 from t3 in table3 where (t1.ID==t22.t1ID & t22.t3ID==t3.ID) select t3) } give right result (much less duplicate results). In spite of we can filter result with 'DISTINCT' operator - difference in performance between two examples is enormous.

select records within a week


i try to use select statement to select records within a week from orderdate. like,


"select * from order where orderdate <= DATEADD(day,7,orderdate)";


correct me if wrong.


SELECT Random Records in term of time



I want to return random records in term of time whithout NEWID

Linq to entities: Always select before delete and update?



With linq to entities, how can I delete and update data inside a database without first selecting the complete entity at first?
The idea behind this is to improve performance.


LINQ select distinct



I have a select like this one:

var x = from t in dc.table
where t.sourceId = 5
order by t.itemId descending
select new { t.itemId, t.sourceId, t.title }

I would select only the records with a unique title and let the other fields like sourceId to have duplicates. How can I do this please ?

Select unique records, take the largest


Suppose, I have a table with the following data.   I need to be able to select the unique records from this table, on the LogicalHash column.  However, I need the select the largest by FileSize.   I can't quite figure out the correct SQL for this.   If I didn't have to worry about taking the largest file, I would just do this:

WHERE FileID in (
SELECT max(FileID) FROM files GROUP BY LogicalHash)


How do pick the largest file?  The file size is not going to be unique throughout the table, I can't select based on that.  Using cursors sounds like a bad idea.  I also need the SQL to be somewhat backwards compatible to MS Access.

Table: files

How to do "Select top 1 customer from customers" in Linq to Sql?


I've gotten half way there:

 var customerList = from c in dc.Customers orderBy c.Sales select c;

 That gets me a collection of customers. How do I use the equivalent of the top operator in SQL?



Linq select null value

from e in Employees
where e.EmployeeID == id
select new {
Sales1 = e.HomeSales.Max(),
Sales2 = e.AwaySales.Max()};

I have to get the maximum of these two select values. The select values are nullable. How do I go about checking if the value is null before
calling the Max() method on it?

I tried the ?? coalesing operator (example: e.HomeSales.Max() ?? 0M,) but I get the following error:

"Operator '??' cannot be applied to operands of type 'decimal' and 'decimal'"

select query for linq on Default.aspx.cs page


how do i create linq query and asign that query to as data source to datalist on default.aspx.cs page

Adding Linq records C#


I haven't quite grasped this in LINQ. I have a two tables called Products and Orders. Products contains a field called orderid. I want to create a new record in Orders and populate orderid with the PK from the new record in Orders. A one to one relationship.


Order neworder = new Order();

creates a new record in Orders.  How do I then tell the Products table what the PK is. If I try something like

Order neworder = new Order();
q.orderid = neworder.OrderID;

It doesn't work.


Select Query to get records within last 24 hours in Nhibernate


Hi All

How to Retrive records within last 24 hrs using Nhibernate.
I want to retrieve the 10 records within last 24 hours.

How can i achieve this using Nhibernate.

Please Help.

FileID FileName LogicalHash DateModified FileSize
1 test.htm 00683b8eb1249ab232f83ab76148127a 11/17/2009 15002
2 document1.doc 00ebb79d95e13e71a0e93e9b82c2edd2 1/5/2008 55623
3 webpage.xml 00de966c7ae802ac9e508b4e0c15f893 10/2/2007
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend