SELECT Meter.SerialNumber as serial,
SupplyPoint.Id as supplypoint,
SupplyType.Id as supplytype,
SupplyType.Name as supplytypename
FROM Meter
INNER JOIN SupplyPoint ON Meter.SupplyPointId = SupplyPoint.Id
INNER JOIN SupplyType ON SupplyPoint.SupplyTypeId = SupplyType.Id;
I have this query so that I might find the Supply Type of a meter based on it's serial. So far I've written this function:
var query = from meters in db.Meters
join supplyPoint in db.SupplyPoints on meters.SupplyPointId
equals supplyPoint.Id
join supplyType in db.SupplyTypes on supplyPoint.SupplyTypeId equals
supplyType.Id
select new { serial = meters.SerialNumber, type = supplyType.Name };
foreach (var meter in query)
{
if (meter.serial == serial)
return meter.type;
}
return "Meter Type Not Specified";`
So I call FindType(string serial) and it returns the type. Can anyone suggest better code for converting such a query? Any directions on where to learn more about LINQ are welcome too.
You are up for reading up quite a bit of material before you should get started.
Read about entity framework here.
Read about LINQ to SQL
here.
Hopefully, once you have you will realize that your problem boils down to the following code.
var result = context.YourTable.FirstOrDefault(r => r.Field == searchValue);
Without knowing much about what you are working with there's little that can be done to give you concrete help. First you want to get familiar with translating the sql statement to LINQ.
LINQ has a query syntax that starts with the from because that is how to define the datasource. SELECT FirstName FROM Authors becomes
from auth in db.Authors
select auth.FirstName
Where db is a reference to a datacontext as suggested in slugster's comment.
You can look for more information on linq at the following url
Related
I am writing a simple query in LINQ. In fact it is my first query in Linq. How the query is interpreted by compiler confused me. My query is not giving any result.
My Linq Query:
using (DataClasses1DataContext db = new DataClasses1DataContext(("connection string")))
{
var notif_sched_data = from sched in db.NOTIF_SCHEDs
join recip in db.NOTIF_RECIPs
on sched.NOTIF_RECIP_ID equals recip.NOTIF_RECIP_ID
select sched.NOTIF_RPT_ID;
}
which is interpreted by compiler as
SELECT [t0].[NOTIF_RPT_ID]
FROM [dbo].[NOTIF_SCHED] AS [t0]
INNER JOIN [dbo].[NOTIF_RECIP] AS [t1] ON [t0].[NOTIF_RECIP_ID] = [t1].[NOTIF_RECIP_ID]
and it gives no row as output. However,the SQL which is correctly working is :
select [NOTIF_SCHED].[NOTIF_RPT_ID]
from [NOTIF_SCHED]
INNER JOIN [NOTIF_RECIP] on [NOTIF_RECIP].[NOTIF_RECIP_ID]=[NOTIF_SCHED].[NOTIF_RECIP_ID].
Can somebody tell me what I need to change?
if i change the value around equals,it gives error as value is not in the right scope.
You are not materializing the query in anyway. the query will return you IQueryable of what you are selecting there. To get the query actually executed to the database you need to materialize the results with methods like .Count(), .FirstOrDefault(), .Tolist(), etc...
using (var db = new DataClasses1DataContext(("connection string")))
{
var query = (from sched in db.NOTIF_SCHEDs
join recip in db.NOTIF_RECIPs
on sched.NOTIF_RECIP_ID equals recip.NOTIF_RECIP_ID
select sched.NOTIF_RPT_ID);
var count = query.Count();
var list = query.ToList();
}
Thanks for your time. Actually query was correct. The problem was in the class file where we convert sql objects in OOPS objects.
i think the problem is for the last line.you should create a new anonymous type.so changing the last line to this may solve the problem :
select new { sched.NOTIF_RPT_ID };
also you can create a class like this one and set your values to an object of the class
public class data
{
public int id {get;set}
public somthing somthing {get;set}
...
}
now simply :
select new { id = sched.NOTIF_RPT_ID , something = x , ... };
I would like to query a DataTable that produces a DataTable that requires a subquery. I am having trouble finding an appropriate example.
This is the subquery in SQL that I would like to create:
SELECT *
FROM SectionDataTable
WHERE SectionDataTable.CourseID = (SELECT SectionDataTable.CourseID
FROM SectionDataTable
WHERE SectionDataTable.SectionID = iSectionID)
I have the SectionID, iSectionID and I would like to return all of the records in the Section table that has the CourseID of the iSectionID.
I can do this using 2 separate queries as shown below, but I think a subquery would be better.
string tstrFilter = createEqualFilterExpression("SectionID", strCriteria);
tdtFiltered = TableInfo.Select(tstrFilter).CopyToDataTable();
iSelectedCourseID = tdtFiltered.AsEnumerable().Select(id => id.Field<int>("CourseID")).FirstOrDefault();
tdtFiltered.Clear();
tstrFilter = createEqualFilterExpression("CourseID", iSelectedCourseID.ToString());
tdtFiltered = TableInfo.Select(tstrFilter).CopyToDataTable();
Although it doesn't answer your question directly, what you are trying to do is much better suited for an inner join:
SELECT *
FROM SectionDataTable S1
INNER JOIN SectionDataTable S2 ON S1.CourseID = S2.CourseID
WHERE S2.SectionID = iSectionID
This then could be modeled very similarily using linq:
var query = from s1 in SectionDataTable
join s2 in SectionDataTable
on s1.CourseID equals s2.CourseID
where s2.SectionID == iSectionID
select s1;
When working in LINQ you have to think of the things a bit differently. Though you can go as per the Miky's suggestion. But personally I would prefer to use the Navigational properties.
For example in your given example I can understand that you have at-least 2 tables,
Course Master
Section Master
One Section must contain a Course reference
Which means
One Course can be in multiple Sections
Now if I see these tables as entities in my model I would see navigational properties as,
Course.Sections //<- Sections is actually a collection
Section.Course //<- Course is an object
So the same query can be written as,
var lstSections = context.Sections.Where(s => s.Course.Sections.Any(c => c.SectionID == iSectionID)).ToList();
I think you main goal is, you are trying extract all the Sections where Courses are same as given Section's Courses.
I've been looking up how to do this, and I found something close, but not quite what I'm looking for. I wonder if this might help others as well, but I could really use the help. I've got a pretty simple SELECT statement I need to convert into LINQ to SQL to speed up searches in our software:
SELECT Animals.*
FROM Animals
INNER JOIN AnimalAliases
ON Animals.AnimalID = AnimalAliases.AnimalID
AND AnimalAliases.Alias LIKE N'%USERINPUT%';
Basically, I want to be able to do a Inner join with multiple conditions, but one of the conditions has nothing to do with one of the tables, the LIKE statement, which is where I get stuck.
var query =
from animal in context.Animals
join animalAlias in context.AnimalAliases
on new { animal.AnimalID, "USERINPUT" }
equals new { animalAlias.AnimalID, animalAlias.Alias }
select animal;
but that doesn't work obviously, because I dont want EQUALS to USERINPUT, I want to perform various LIKE operations on it..
Anyone have any insight?
I think what you're looking for is just a normal WHERE clause?
var query = from animal in context.Animals
join animalAlias in context.AnimalAliases on animal.AnimalID equals animalAlias.AnimalID
where animalAlias.Alias.Contains(USERINPUT)
select animal;
The alias text is not part of the foreign key relationship - so it shouldn't be in the join.
Update - after comments - including from #Steven - while the query above is correct and does mimic the original SQL - it might be wise to use Distinct() on the output - this will prevent the case where multiple alias matches could occur for a single animal.
var query = (from animal in context.Animals
join animalAlias in context.AnimalAliases on animal.AnimalID equals animalAlias.AnimalID
where animalAlias.Alias.Contains(USERINPUT)
select animal).Distinct();
This will do the trick:
IQueryable<Animal> query =
from animal in context.Animals
where animal.AnimalAliases
.Any(a => a.Alias.Contains("USERINPUT"))
select animal;
Alternatively, you can do it the other way around (start at the AnimalAlias entity instead of starting at the Animal entity):
IQueryable<Animal> query = (
from animalAlias in context.AnimalAliases
where animalAlias.Alias.Contains("USERINPUT")
select animalAlias.Animal)
.Distinct();
I have the following LINQ query, that is returning the results that I expect, but it does not "feel" right.
Basically it is a left join. I need ALL records from the UserProfile table.
Then the LastWinnerDate is a single record from the winner table (possible multiple records) indicating the DateTime the last record was entered in that table for the user.
WinnerCount is the number of records for the user in the winner table (possible multiple records).
Video1 is basically a bool indicating there is, or is not a record for the user in the winner table matching on a third table Objective (should be 1 or 0 rows).
Quiz1 is same as Video 1 matching another record from Objective Table (should be 1 or 0 rows).
Video and Quiz is repeated 12 times because it is for a report to be displayed to a user listing all user records and indicate if they have met the objectives.
var objectiveIds = new List<int>();
objectiveIds.AddRange(GetObjectiveIds(objectiveName, false));
var q =
from up in MetaData.UserProfile
select new RankingDTO
{
UserId = up.UserID,
FirstName = up.FirstName,
LastName = up.LastName,
LastWinnerDate = (
from winner in MetaData.Winner
where objectiveIds.Contains(winner.ObjectiveID)
where winner.Active
where winner.UserID == up.UserID
orderby winner.CreatedOn descending
select winner.CreatedOn).First(),
WinnerCount = (
from winner in MetaData.Winner
where objectiveIds.Contains(winner.ObjectiveID)
where winner.Active
where winner.UserID == up.UserID
orderby winner.CreatedOn descending
select winner).Count(),
Video1 = (
from winner in MetaData.Winner
join o in MetaData.Objective on winner.ObjectiveID equals o.ObjectiveID
where o.ObjectiveNm == Constants.Promotions.SecVideo1
where winner.Active
where winner.UserID == up.UserID
select winner).Count(),
Quiz1 = (
from winner2 in MetaData.Winner
join o2 in MetaData.Objective on winner2.ObjectiveID equals o2.ObjectiveID
where o2.ObjectiveNm == Constants.Promotions.SecQuiz1
where winner2.Active
where winner2.UserID == up.UserID
select winner2).Count(),
};
You're repeating join winners table part several times. In order to avoid it you can break it into several consequent Selects. So instead of having one huge select, you can make two selects with lesser code. In your example I would first of all select winner2 variable before selecting other result properties:
var q1 =
from up in MetaData.UserProfile
select new {up,
winners = from winner in MetaData.Winner
where winner.Active
where winner.UserID == up.UserID
select winner};
var q = from upWinnerPair in q1
select new RankingDTO
{
UserId = upWinnerPair.up.UserID,
FirstName = upWinnerPair.up.FirstName,
LastName = upWinnerPair.up.LastName,
LastWinnerDate = /* Here you will have more simple and less repeatable code
using winners collection from "upWinnerPair.winners"*/
The query itself is pretty simple: just a main outer query and a series of subselects to retrieve actual column data. While it's not the most efficient means of querying the data you're after (joins and using windowing functions will likely get you better performance), it's the only real way to represent that query using either the query or expression syntax (windowing functions in SQL have no mapping in LINQ or the LINQ-supporting extension methods).
Note that you aren't doing any actual outer joins (left or right) in your code; you're creating subqueries to retrieve the column data. It might be worth looking at the actual SQL being generated by your query. You don't specify which ORM you're using (which would determine how to examine it client-side) or which database you're using (which would determine how to examine it server-side).
If you're using the ADO.NET Entity Framework, you can cast your query to an ObjectQuery and call ToTraceString().
If you're using SQL Server, you can use SQL Server Profiler (assuming you have access to it) to view the SQL being executed, or you can run a trace manually to do the same thing.
To perform an outer join in LINQ query syntax, do this:
Assuming we have two sources alpha and beta, each having a common Id property, you can select from alpha and perform a left join on beta in this way:
from a in alpha
join btemp in beta on a.Id equals btemp.Id into bleft
from b in bleft.DefaultIfEmpty()
select new { IdA = a.Id, IdB = b.Id }
Admittedly, the syntax is a little oblique. Nonetheless, it works and will be translated into something like this in SQL:
select
a.Id as IdA,
b.Id as Idb
from alpha a
left join beta b on a.Id = b.Id
It looks fine to me, though I could see why the multiple sub-queries could trigger inefficiency worries in the eyes of a coder.
Take a look at what SQL is produced though (I'm guessing you're running this against a database source from your saying "table" above), before you start worrying about that. The query providers can be pretty good at producing nice efficient SQL that in turn produces a good underlying database query, and if that's happening, then happy days (it will also give you another view on being sure of the correctness).
Sorry about the vague title, not sure what verbage I should be using. I have a query similar to this (re-worked to save space):
SELECT
*
FROM
Publishers p
INNER JOIN Authors a
ON p.AuthorID = a.AuthorID
INNER JOIN Books b
ON a.BookID = b.BookID
WHERE
p.PublisherName = 'Foo'
ORDER BY
b.PublicationDate DESC
I tried to re-write it as such:
var query =
from publisher in ctx.Publishers
from author in publisher.Authors
from books in author.Books
...
but got the following error:
Error 1 An expression of type 'Models.Books' is not allowed in a
subsequent from clause in a query expression with source type
'System.Linq.IQueryable<AnonymousType#1>'. Type inference failed in the
call to 'SelectMany'.
I can re-write the LINQ to make it work by just joining the tables, as I would in SQL, but I thought I could accomplish what I want to do by their relationships - I'm just a bit confused why I can get publisher.Authors, but not author.Books.
Check that you have a relationship in your DB from Authors to Books.
Try this...
var result = (from pItem in ctx.Publishers
join aItem in ctx.Authors on pItem.AuthorId equals aItem.AuthorId
join bItem in ctx.Books on pItem.BookId equals bItem.BookId
where pItem.PublisherName== "Foo"
select new {
// Fields you want to select
}
).ToList();
i don't know exact relationship of the tables but you can an idea from this one.