LINQ:
Is it more efficient to use the Single() operator over First() when ever I know for certain that the query will return a single record?
Is there a difference?
Hi,
Is it possible to return a single value and an enumerable collection using LINQ to SQL?
The problem is, I'm trying to do paging across a large recordset. I only want to return 10 rows at a time so I'm using .Skip(20).Take(10) approach.
However I need to know the total number of records so I can show an appropriate page x of y.
Trying to avoid two separate queries.
Thanks
I have extended some functionality to a DataContext object (called "CodeLookupAccessDataContext") such that the object exposes some methods to return results of LINQ to SQL queries. Here are the methods I have defined:
public List<CompositeSIDMap> lookupCompositeSIDMap(int regionId, int marketId)
{
var sidGroupId = CompositeSIDGroupMaps.Where(x => x.RegionID.Equals(regionId) && x.MarketID.Equals(marketId))
.Select(x => x.CompositeSIDGroup);
IEnumerator<int> sidGroupIdEnum = sidGroupId.GetEnumerator();
if (sidGroupIdEnum.MoveNext())
return lookupCodeInfo<CompositeSIDMap, CompositeSIDMap>(x => x.CompositeSIDGroup.Equals(sidGroupIdEnum.Current), x => x);
else
return null;
}
private List<TResult> lookupCodeInfo<T, TResult>(Func<T, bool> compLambda, Func<T, TResult> selectLambda)
where T : class
{
System.Data.Linq.Table<T> dataTable = this.GetTable<T>();
var codeQueryResult = dataTable.Where(compLambda)
.Select(selectLambda);
List<TResult> codeList = new List<TResult>();
foreach (TResult row in codeQueryResult)
codeList.Add(row);
return codeList;
}
CompositeSIDGroupMap and CompositeSIDMap are both tables in our database that are represented as objects in my DataContext object. I wrote the following code to call these methods and display the T-SQL generated after calling these methods:
using (CodeLookupAccessDataContext codeLookup = new CodeLookupAccessDataContext())
{
codeLookup.Log = Console.Out;
List<CompositeSIDMap> compList = codeLookup.lookupCompositeSIDMap(5, 3);
}
I got the following results in my log after invoking this code:
SELECT [t0].[CompositeSIDGroup]
FROM [dbo].[CompositeSIDGroupMap] AS [t0]
WHERE ([t0].[RegionID] = @p0) AND ([t0].[MarketID] = @p1)
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [5]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [3]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
SELECT [t0].[PK_CSM], [t0].[CompositeSIDGroup], [t0].[InputSID], [t0].[TargetSID], [t0].[StartOffset], [t0].[EndOffset], [t0].[Scale]
FROM [dbo].[CompositeSIDMap] AS [t0]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
The first T-SQL statement contains a where clause as specified and returns one column as expected. However, the second statement is missing a where clause and returns all columns, even though I did specify which rows I wanted to view and which columns were of interest. Why is the second T-SQL statement generated the way it is, and what should I do to ensure that I filter out the data according to specifications via the T-SQL?
Also note that I would prefer to keep lookupCodeInfo() and especially am interested in keeping it enabled to accept lambda functions for specifying which rows/columns to return.
I have Linq to Entity query like you can see below I am using it five times in my code, everything that change is where clause. is it possible to create a method and pass just where values, not to write all code five times. Thank you
items = from t1 in _entities.table1
join t2 in _entities.Table2 on t1.column1 equals t2.column1
join t3 in _entities.Table3 on t1.column2 equals t3.column2
join t4 in _entities.Table4 on t1.column3 equals t4.column3
where **t1.column5 == Something**
select new
{
t1.column7,
t2.column8,
t3.column9,
t4.column10
};
I'm in a situation where I want to add the equivalent of the sql statement
SET QUERY_GOVERNOR_COST_LIMIT
to my query I created with linq to entities.
How would I go about that?
Hello everyone,
I am using Linq-to-Sql for a C# application and am currently working on some stored procedures. The application is for a newspaper, and a sample stored procedure is the following:
ALTER PROCEDURE dbo.Articles_GetArticlesByPublication
@publicationDate date
AS
SELECT
*
FROM
Articles
WHERE
Articles.PublicationDate=@publicationDate
Anyway, this query gets all of the articles where the publication date is equal to the argument (publicationDate). How can I alter this so that the argument can handle multiple publication dates?
Also, I'd prefer not to use "BETWEEN," rather, I want to pick and choose dates.
Can any one tell me the LINQ 2 SQL version (in vb.net) of the below Left outer join query.I am trying to get all Employees with name "Shyju" and their address line 1 if it exist in the address table
SELECT E.EMPLOYEE_NAME,
E.AGE,A.ADRESS_LINE1
FROM EMPLOYEE_MASTER E
LEFT OUTER JOIN
ADDRESS_MASTER A
ON E.ID=A.EMPLOYEE_ID
WHERE E.EMPLOYEE_NAME='Shyju'
Thanks in advance
Hi
i've just started reading up on PLINQ and find it fasinating.
I'm using NHib-Linq in my projects - does anyone know if there's any benefit/problems using PLINQ type queries with NHLinq?
w://
I am using LINQ expressions in my code
like this
var obj = Collection.Single(collection = (collection.ShortName.Equals("AAA")));
The problem is that this line works fine for me, no problems.
But when I upload the same executable to some remote machine with same 32 bit Windows XP. The code execution is just stopping at this line of source.
Can anyone help me.
Hi all,
i am having query for update the node value using Linq,
For example i am have to update
<Student>
<studentdetail>
<studentname>test</studentname>
<libraryid>hem001</libraryid>
</studentdetail>
</Student>
in above xml i want to change the value of Student name "test" ti something else like "Undertest"
regards
NewDev
Hi,
I have object A which contains multiple instances of object B, which in turn contains multiple instances of object C. I need to write a function which, given Object A needs search through instances of objects B and objects C and find a particular object C. How would I do this using LINQ?
Hi,
Is there any way to cache LINQ to SQL queries by looking at the parameters that were previously passed and bypass the database all together?
I know L2S caches some database calls, but I'm looking for a permanant solution as in, even if the applciation restarts, that cache reloads and never asks the database again.
Are there any frameworks for C#?
Reading about both Linq to SQL and Entity Framework I have developed the impression that EF is more suitable for apps that get data from multiple data sources.
But as I am reading about MVC2 models I see an example where EF is more loosely coupled with your data model.
If I have to add or remove some columns from a table then what is involved in updating my model or DAL with each of these implementations?
How can i rewrite the below SQL query to its equivalent LINQ 2 SQL expression (both in C# and VB.NET)
SELECT t1.itemnmbr, t1.locncode,t1.bin,t2.Total
FROM IV00200 t1 (NOLOCK)
INNER JOIN
IV00112 t2 (NOLOCK)
ON t1.itemnmbr = t2.itemnmbr
AND t1.bin = t2.bin
AND t1.bin = 'MU7I336A80'
Hi All,
i would like to iterate over the items of a List<T>, except the first, preserving the order. Is there an elegant way to do it with LINQ using a statement like:
foreach (var item in list.Skip(1).TakeTheRest())
{....
I played around with TakeWhile , but was not successful. Probably there is also another, simple way of doing it?
Can anyone answer me what are the differences of
Session.Query
Session.Linq and
Session.QueryOver
What I'm really interested in:
What would be supported in the future versions.
What should I start to use in a clean project.
Please tell me your thoughts about these three...
Thanks,
Zoltán
Using Linq to SQL:
if(!parentlist.childlist.Contains(row1))
parentlist.childlist.Add(row1);
else
How do I update the required childlist row with row1? Each row of the child list has a unique id.
parentlist implements IEnumerable and childlist is IList.
I have multiple C# projects in a Visual Studio solution right now that will all use the same SQL Server database.
What is the proper way to share LINQ-to-SQL classes between projects?
I'm considering just copying the dmbl files into each project, but I think that may be too redundant. Is there a better way to approach this?
If I have two tables; Drivers keyed by DriverId and Trips with foreign keys DriverId and CoDriverId, and I want to find all trips where a driver was either the driver or co-driver I could code this in Transact-SQL as
select d.DriverId, t.TripId
from Trips t inner join Drivers d
on t.DriverId = d.DriverId or t.CoDriverId = d.DriverId
How could this be coded as a LINQ query?
I am using VS2010 and C#
When I map/select my database tables with LINQ to SQL I have to option to change the "member" propery, but when i delete the table (because I changed something in the schema for example) and add it again the member value gets "reset". Is it possible to set/override this member programmaticly, so that I dont have to change it by hand everytime
I mean the member option of
'<'Table Name="dbo.table1" Member="table1"
If I want to get a user that has the email address of '[email protected]', how do I pass that as a parameter in linq?
ie.:
var a = from u in Users
where u.Email = @email
Select u;
So this would be used in my method:
public static GetuserByEmail(string email)
Do I just pass in the variable or?
I would like to use Linq instead of below function :
Friend Function IsCollectionInTable2(ByVal apps As DataTable, ByVal collectionId As String) As Boolean
For Each row As DataRow In apps.Rows
If row("CollectionId").ToString = collectionId Then Return True
Next
Return False
End Function
The best I can do is below:
Friend Function IsCollectionInTable(ByVal apps As DataTable, ByVal collectionId As String) As Boolean
Return (From row In apps.AsEnumerable()
Where (row.Field(Of String)("CollectionId") = collectionId)
Select row.Field(Of String)("CollectionId")).Count > 0
End Function
I would like to use Exists or Any in above function. Performance could be an issue,
I have struggled converting this SQL statement to LINQ to SQL VB.Net 9.0. I have used Linqer but no success. Any help would be appreciated
select t.TeeId,
t.DescriptionId,
t.[Description],
t.Rating,
t.Slope,
case when d.TotalHoles <> h.TotalHoles then 0
else 1 end [Status]
from dbo.CourseDescription d
inner join dbo.CourseTees t
on t.DescriptionId = d.DescriptionId
inner join (select TeeId, count(*) as TotalHoles
from dbo.CourseHoles
group by TeeId) h
on h.TeeId = t.TeeId
where d.CourseId = 1
I'd like to do the equivalent of the following in LINQ, but I can't figure out how:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
What is the real syntax?