ASP.NET: Linq2SQL: selecting all names matching an id - asp.net

Got 2 tables: db.Tags (ID, TagName) and db.Names (ID, Name, TagID).
I want to fetch all db.Tags rows, and all the Names matching the TagID.
So it will look like
ID - TagName - Names
1 - tag1 - name1, name2, name3
2 - tag2 - name4, name5, name6
Is this possible in one (long) linq query?
or do I have to get all the tags, then do foreach tag, get all the names, then do foreach names to put them in a one long string..
Thanks in advance!
EDIT:
Okay see my comment on the second answer (first one up..), this is what i tried but i get some errors in compiler:
var tags =
from t in db.Tags
orderby t.Priority ascending
select new {
t.ID,
t.Name,
t.Priority,
Places = String.Join(", ",
(from p in db.Places
join o in db.TagToPlaces on new {
p.ID,
t.ID
}
equals new {
o.PlaceId,
o.TagId
}
select p.Name
).ToArray()
)
}
);

I think this is what you're after:
var query =
from t in db.Tags
select new
{
t.ID,
t.TagName,
Names = String.Join(", ",
(from n in db.Names
where n.TagID == t.ID
select n.Name)
.ToArray()),
};
With this I get the same sort of output that you gave in your question. I also understood that you want to output the tag id and name even when there are no associated name records - my query does that.
Now depending on if you're using EF or LINQ-to-SQL or something else you may need to add .ToArray() to the db.Tags & db.Names references to force the database query to occur.
If you have a large number of tag records you'll find you have a large number of queries going to the database. You could make this change to reduce it to only two queries:
var tags = db.Tags.ToArray();
var names = db.Names.ToArray();
var query =
from t in tags
select new
{
t.ID,
t.TagName,
Names = String.Join(", ",
(from n in names
where n.TagID == t.ID
select n.Name)
.ToArray()),
};
Now you just need to make sure that your data fits into memory - but it sounds like it should. I hope this helps.

Since the concat is a pain in TSQL, I would query the 3 values "as is", and format from there:
var list = (from tag in db.Tags
join name in db.Names
on tag.ID equals name.TagId
orderby tag.ID
select new { tag.ID, tag.TagName, name.Name }).ToList();
for example, if I wanted the names by tag-id, I could do:
var namesByTag = list.ToLookup(row => row.ID, row => row.Name);
(or whatever else you choose)

Related

Sqlite select default rows if not exists

Sorry to pester with a simple problem but I'm stumped with a simple select on an HTML5 WebSQL Data Base.
Table tPhones has id, hid, location and several other columns. I would like to return a list of rows with where hid = [input value] or, if no rows with that hid exist, return rows where hid = 1.
I have tried LIMIT 1 ACS etc but that too fails.
Function showPhones(){
var phonegroup = $(this).attr("id");
var hid = localStorage.hid;
db.transaction (function(transaction)
{
var sql = "SELECT * FROM tPhones WHERE dept ='"+phonegroup+"' AND ((hid ='"+hid+"') OR IFNULL (hid ='1')) ORDER BY location ASC";
transaction.executeSql (sql, undefined,function(transaction,result)
{…………..rest of function working fine
Any help would be much appreciated.
A normal expression in the WHERE clause can access only columns of the current record.
To check for the existence of any other record, you have to use a subquery:
SELECT *
FROM tPhones
WHERE dept = ?
AND (hid = ? OR (hid = 1 AND
NOT EXISTS (SELECT 1
FROM tPhones
WHERE dept = ?
AND hid = ?)))
ORDER BY location

Search using linq and dropdowns

I got about 5 look-a-like linq querys just like this SortPerson() metod. I'm trying to develop a search using dropdowns where a user can select values from the dropdown and returns the values that are true from one or more dropdowns the user has selected to use.
Is there a simpler way to develop this? help would be much appreciated
public void SortPerson()
{
var personId = ddlPerson.SelectedValue;
var data = new MyModelContext();
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
select d;
if (!String.IsNullOrEmpty(personId))
{
documents = documents.Where(c => c.DocPerson.Equals(personId));
}
rptResult.DataSource = documents.ToList();
rptResult.DataBind();
}
I don't see the point in joining without Where if you still select only one table.
If you want all document in case when Person is not selected, then you can't create much simpler method. You can write it shorter like:
var documents =
from d in data.tblDocuments
join ...
where String.IsNullOrEmpty(personId) || d.DocPerson equals personId
select d;
so you don't need separate if statement.
If you want to use several values from 5 dropdowns and use them as conditions in single query, just add more conditions:
var personId = ddlPerson.SelectedValue;
var someValue = ddlSomeDDL.SelectedValue;
//3 more values from DDL
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
where (String.IsNullOrEmpty(personId) || sp.PersonId equals personId)
&& (String.IsNullOrEmpty(someValue) || d.SomeColumn equals someValue)
//3 more conditions
select d;

Is there a way to select a columns from a joined table without explicitly listing all columns?

I'm trying to use JoinSqlBuilder to select a data from one of the joined tables, and can't find a way to do that unless I list all columns from that table. Hopefully I'm missing something and it actually can be done.
This is approximately what I have:
var sql = new JoinSqlBuilder<Product, Product>()
.Join<Product, Customer>(src => src.Id, dst => dst.Id)
.Where<Customer>(x => x.Id == Id);
and I want to select everything from a product table. The query above throws an exception complaining about column name collisions, so its clearly does a select from both tables.
Edit: In the end I want to have this sql (never mind the design, its not a real thing):
select
p.* //<-- This is the piece that I'm struggling with
from product p inner join customer c on p.id on c.productId
where blah;
Looks like OrmLite want me to explicitly list all columns I want to return, which I want to avoid.
Note: I'm using 3.9.71 of servicestack. I've not looked at the 4.0 implementation yet.
I think you have a FK relationship problem with your join. Assuming that a product has a customer FK named (CustID), it'd look like this. Additionally, you'd need a POCO to represent the result set, if you are returning a "combination" of the results. I don't think you'll want to return both "ID" columns, and instead return a FK column.
return _factory.Run<ProductCustomer>(conn=>
{
var jn = new JoinSqlBuilder<Product, Customer>();
jn = jn.Join<Product, Customer>(srcProd => srcProd.CustId,
dstCust => dstCust.Id, // set up join Customer.id -> Product.CustId
p => new { p.Id, p.OtherField}, // product table fields returned
c => new { c.Name, c.AddressId}, // customer fields returned
null, //where clause on the product table
cust=>cust.Id = customerId // where clause on the customer table
);
var sql = jn.ToSQL();
return conn.FirstOrDefault<ProductCustomer>(sql);
}
Hope this helps.
Edit: After your Edit, try this:
// set up join Customer.id -> c.ProductId
jn = jn.Join<Product, Customer>(srcProd => srcProd.Id, dstCust => dstCust.productId)
.Where<Customer>(c=>c.Id == custIdParameter);
var sql = jn.ToSql();
You can add a ".Where" again for the
Where<Product>(p=>p.id == foo);
if you need to add more product with your BLAH. This should get you close.
Have you tried the SelectAll extension method?
var sql = new JoinSqlBuilder<Product, Product>()
.Join<Product, Customer>(src => src.Id, dst => dst.Id)
.SelectAll<Product>()
.Where<Customer>(x => x.Id == Id);

LINQ - EF join difficulty

I have two tables:
Phase :
long ID
string Name
and another Activity :
long ID
string Name
long PhaseID
I already know the name of the phases and I want to get the activity for those particular phases. Do i add PhaseName to the activity table or do I do it through join in LINQ?
Maybe something like this?
var query = from a in entities.Activities
join p in entities.Phases on a.PhaseId equals p.Id
where p.Name == "Preplanning"
... and here im not sure how to finish this query..
Thanks for your help!
The code that you've provided will use an Inner Join to find all Activities where the Phase with Name "Preplanning" exists.
To finish your query you need to add a select clause.
var query = from a in entities.Activities
join p in entities.Phases on a.PhaseId equals p.Id
where p.Name == "Preplanning"
select a.Name
will return IEnumerable<string> of all activity names.
Just select activity, as you want:
var query = from a in entities.Activities
join p in entities.Phases on a.PhaseId equals p.Id
where p.Name == "Preplanning"
select a;
Here is how query expression should look like:
A query expression must begin with a from clause and must end with a select or group clause. Between the first from clause and the last select or group clause, it can contain one or more of these optional clauses: where, orderby, join, let and even additional from clauses. You can also use the into keyword to enable the result of a join or group clause to serve as the source for additional query clauses in the same query expression.
Same as puzzling image:
With method syntax you don't need to end query with something special:
var query = entities.Phases
.Where(p => p.Name == "Preplanning")
.Join(entities.Activities, p => p.Id, a => a.PhaseId, (p,a) => a);
No need to do a join if you only need data from one of the tables. You can apply a filter instead:
var q = entities.Activities.Where(a =>
entities.Phases.Any(p => a.PhaseId == p.Id && p.Name == "Preplanning"));

JOIN for Membership table

Why Microsoft uses stupid examples with GroupBy clause only for one table?
http://msdn.microsoft.com/en-us/library/bb896341.aspx
And why error is causing when i tried to Select some field that is not in GroupBy?
Error says that fields that is not in GroupBy clause doesn't exist in current context.
using (Entities db = new Entities())
{
var query = "SELECT RLS.RoleId, UIR.UserId, UIR.RoleId, USR.UserName FROM Entities.aspnet_Roles AS RLS " +
"INNER JOIN Entities.vw_aspnet_UsersInRoles AS UIR " +
"ON RLS.RoleId = UIR.RoleId " +
"INNER JOIN Entities.aspnet_Users AS USR " +
"ON UIR.UserId = USR.UserId " +
//"GROUP BY RLS.RoleId" // is uncomment - it says UserId doesn't exist in context
;
var x = new ObjectQuery<DbDataRecord>(query, db);
var y = x.ToTraceString().Replace("\n", " ").Replace("\t", " ").Replace("\r", " ");
}
The same for LINQ:
var x = from RLS in db.aspnet_Roles
join URS in db.vw_aspnet_UsersInRoles
on RLS.RoleId equals URS.RoleId
join USR in db.aspnet_Users
on URS.UserId equals USR.UserId
group RLS by RLS.RoleId into GRP
select new
{
GRP.Key
};
The question is: how to select all fields and GroupBy selection by only one field?
Thanks in advance.
All fields that are not in the GROUP BY clause have to be aggregate expressions such as AVG(...) or SUM(...) or MAX(...) etc.
So this is wrong:
SELECT RLS.RoleId, UIR.UserId, UIR.RoleId, USR.UserName
(...)
GROUP BY RLS.RoleId
because the last three expressions in the SELECT should either be aggregated somehow or added to the GROUP BY clause.
This is logical because by specifying GROUP BY RLS.RoleId you say that RLS.RoleId should be unique in the end result, so only one row per value. But for each RLS.RoleID there could be multiple values for UIR.UserId, UIR.RoleId or USR.UserName. So you need to tell the DBMS what to do to make them into one row: average, maximum, minimum... OR you add fields to the GROUP BY clause, so that not RLS.RoleID but the combination of column values should be unique. See also this example.
I don't see any aggregate functions (COUNT, SUM, AVG, etc) being used in the query you posted. Thus, there's no need to include a GROUP BY expression, and there's really no reason to do so. Could you please explain what you're trying to accomplish?
I think i didn't understand littlegreen.
His advice really helped ... though i was upset by SQL Server comparing to MySQL (there is enough to set one grouping field).
Works!
SELECT DISTINCT
RLS.RoleId,
COUNT(RLS.RoleId) AS RID_CNT,
MAX(CAST(UIR.UserId AS VARCHAR(36))) AS EXPR1,
MAX(CAST(USR.UserName AS VARCHAR(36))) AS EXPR2
FROM aspnet_Roles AS RLS
INNER JOIN vw_aspnet_UsersInRoles AS UIR ON RLS.RoleId = UIR.RoleId
INNER JOIN aspnet_Users AS USR ON UIR.UserId = USR.UserId
GROUP BY RLS.RoleId

Resources