Good morning SO.
I have 3 tables (Sql Server) with this layout:
GenreMovies: | RowID | MovieGenre|
GenreMusic: |RowID | MusicGenre |
GenrePodcast:| RowID | PodcastGenre|
I have a combobox/dropdown that I am trying to populate with a Query:
var infoQuery =
(from MovieGen in dbContext.GenreMovies
select MovieGen.RowID)
.Union
(from MusicGen in dbContext.GenreMusics
select MusicGen.RowID).Union(from PodcastGen in dbContext.GenrePodcasts select PodcastGen.RowID).ToList();
GridSortSearch.DataTextField = "MovieGenre";
GridSortSearch.DataSource = infoQuery;
GridSortSearch.DataBind();
I have two issues the query is only being populated with "RowID"
and the combobox/dropdown is failing at the "DataTextField"
Like I said you need to return the same rows on results to achieve what you want.
Something like this, notice the change to the selects and the name of DataTextField value.
var infoQuery =
(from MovieGen in dbContext.GenreMovies
select new { MovieGen.RowID, Genre=MovieGen.MovieGenre})
.Union
(from MusicGen in dbContext.GenreMusics
select new {MusicGen.RowID, Genre=MusicGen.MusicGenre).Union(from PodcastGen in dbContext.GenrePodcasts select new {PodcastGen.RowID, Genre=PodcastGen.PodcastGenre).ToList();
GridSortSearch.DataTextField = "Genre";
GridSortSearch.DataSource = infoQuery;
GridSortSearch.DataBind();
I couldn't compile/test this where I am at the moment but should work.
Related
I have the following data:
Id | Value | OtherStuff
---------------------------
6 | 6 | 1
---------------------------
5 | 4 | 2
---------------------------
5 | 2 | 3
The desired result:
Id | Value | OtherStuff
---------------------------
6 | 6 | 1
---------------------------
5 | 4 | 2
That is I need the Max Value for each of the Id's.
I'm a bit stumped of how to do this without breaking it into multiple queries, can it be done, and if so how?
Update: I think I oversimplified the issue:
var query = from st in StockStakes
join o in Organisations on j.OrganisationId equals o.OrganisationId into oGroup
from o in oGroup.DefaultIfEmpty()
where st.Stock.Status == "A"
select new
{
Id = st.Id,
Value = st.Value,
CustomerId = o.OrganisationId
};
The data sample from above still stands... now how do i structure the query to give me the Max Value alongside each Id?
var query = from x in data
group x by x.Id into x
select x.OrderByDescending(y => y.Value).FirstOrDefault()
Based on you updated query, similar approach to the first query, but since you have multiple tables you need to group all the tables into an anonymous object and then select only the columns you want
var query = from st in StockStakes
join o in Organisations on j.OrganisationId equals o.OrganisationId into oGroup
from o in oGroup.DefaultIfEmpty()
where st.Stock.Status == "A"
group new { st, o } by st.Id into g
let largestValue = g.OrderByDescending(x => x.Value).FirstOrDefault()
select new
{
Id = g.Key,
Value = largestValue.st.Value,
CustomerId = largestValue.o.OrganisationId
};
I'm not really sure about what you mean, but maybe you can try with this query.
select Id, max(Value)
from your_table
group by Id;
This gives you the max "Value column" value for each "Id column" value.
-- EDIT --
LINQ version:
var q = from t in dc.YourTable
group t by t.Id
into g
select new
{
Id = g.Id,
Value = (from t2 in g select t2.Value).Max()
};
Code not tested. I'm on the bus now... :-) Give it a try!
I am trying to select everything from a table and then make a join with two other tables, from which I need just some of the columns. The final list will be populated into a DevExpress GridControl. The main table that I take the data from has these columns:
Id | ScheduleId | AccountId | Description
I can easily select everything from this table and return. Here is my list:
public IList<StatusDescription> getStatusDescription()
{
var listOfItems = from e in context.StatusDescription select e;
return listOfItems.ToList<StatusDescription>();
}
The other two tables are Schedules and Accounts, they look as follows:
Id | ScheduleName | DateFrom | DateTo
and
Id | AccountName | AccountCode
I would like to join the DateFrom and DateTo from the Schedule table and the AccountCode from the Account table. Is it possible to get all that into a single list. I can easily bind it later to the GridControl
var listOfItems = from e in context.StatusDescription
join s in context.Schedules on e.ScheduleId equals s.Id
join a in context.Accounts on e.AccountId equals a.Id
select new
{
Description = e.Description,
ScheduleStart = s.DateFrom,
ScheduleEnd = s.DateTo,
AccountCode = a.AccountCode
}
var Query = from p in context.StatusDescription
select new
{
p.Id,
p.Description,
p.Schedule.DateFrom,
p.Schedule.DateTo,
p.Account.AccountCode
};
hope it helps
I have this table (Prefrences_Table)
--------------------------
|student | Preferences |
--------------------------
Stud A | Stud B
Stud A | Stud C
Stud B | Stud E
Stud B | Stud A
Stud C | Stud F
Stud F | Stud B
--------------------------
If "Stud A" has added "Stud B" in his Preferences list, i would like to check if "stud B" has also added "stud A" in his preference, so i can add both of them in one group.
How can this be done using SQL or C#?
A self-join should work just fine here. The additional predicate returns only the first instance of the match to avoid duplicates.
select t.student, t1.student
from
Prefrences_Table t
inner join Prefrences_Table t1
on t.student = t1.preferences
and t.preferences = t1.student
and t.student < t1.student
this might give you anwer to your question, field mutual will be one if both students added the other in preferences, zero otherwise
SELECT T1.student, T2.Preferences,
(SELECT COUNT(*) FROM Prefrences_Table T2 WHERE T2.Preferences = T1.student AND T2.student = T1.Preferences) AS mutual
FROM Prefrences_Table T1
Another alternative would be the following:
SELECT * FROM
(
SELECT PM.student, PM.Preferences,
(SELECT COUNT(student) FROM Prefrences_Table AS PI WHERE PI.Preferences = PM.student
AND PI.student = PM.Preferences) AS CheckCross
FROM Prefrences_Table AS PM
) AS PD
WHERE PD.CheckCross > 0
You have some SQL answers, here is one in c#/linq.
var list = new List<Prefrences_Table>();
var results = (from t in list
join t1 in list on t.student equals t1.preferences
where
t.student == t1.preferences &&
t.preferences == t1.student &&
string.CompareOrdinal(t.student, t1.student) < 0
select new {t.student, t1.student}
);
I have a Table: Material(ID,Name,MAterialParentID)
SELECT c1.ID,c1.Name as ParentName,c2.id,c2.Name
FROM Material c1 Left JOIN Material c2
ON c1.ID = c2.MaterialParentID
ID ParentName id Name
1 Aluminium 2 Cavity
1 Aluminium 3 Copper
1 Aluminium 4 Flooring
2 Cavity NULL NULL
3 Copper NULL NULL
4 Flooring NULL NULL
5 Glass NULL NULL
I want to convert the above SQL Query to Linq Query using Liq to Entities.
Help Appreciated!
if the table is only for reading you could simply create a view and then when using reverse engineering make sure you have views imported.
or if you did want this done in LINQ here is the MSDN example
var innerJoinQuery =
from cust in customers
join dist in distributors on cust.City equals dist.City
select new { CustomerName = cust.Name, DistributorName = dist.Name };
this is how yours would look
var Material = from M in db.Materials
join M2 in db.Materials on M.ID equals M2.MaterialParentID
select new {ParentID = M.ID, ParentName = M.Name, M2.ID, M2.Name };
i have edited my post above as you can see i have included the ParentID to make all columns unique
For this kind of problem linqpad is your friend.
I'd suggest something like:
var materials = (from m in context.Material
let moreMaterials = (from m2 in context.Material where m2.id == m.id select m2).FirstOrDefault()
select m).ToList();
But you can use linqpad to customise to your query requirements.
I have a stored procedure that is returning data in this format:
EmployeeID | DepartmentID
---------------------
1 | 1
2 | 1
3 | 2
4 | 4
5 | 4
I'm getting the results like so:
List<spResult> results = DataContext.sp().ToList();
I'd like to get a list of Employees for a certain Department, based on the data returned from the stored procedure. Something like:
int departmentId = 1;
List<Employee> employees = (from e in DataContext.Employees
//where...
select e).ToList();
How do I format my where clause to get the EmployeeIDs from the result set that have the given DepartmentID?
How about:
List<spResult> results = DataContext.sp().ToList();
int departmentId = 1;
var departmentEmployees = from de in results
where de.DepartmentId == departmentId
select de.EmployeeID;
List<Employee> employees = (from e in DataContext.Employees
where departmentEmployees.Contains(e.ID)
select e).ToList();
You could get a subset of keys:
var empKeys = results.Where(i => i.DepartmentID = departmentID);
And then use this list in the query like:
List<Employee> employees = (from e in DataContext.Employees
where empKeys.Contains(e.EmployeeID)
select h).ToList();
HTH.
You should also be able to do something like this:
List<Employee> employees = DataContext.Employees.Where(e => empKeys.Contains(e.EmployeeID)).ToList()