I like to use linq to do a union on 3 tables.
Not sure why something like the following would not work:
var repdata = (from p in db.Table1
select p)
.Union(from p in Table2
select p);
Union only works with the same element type. You could use:
var data = db.Table1.Select(p => new { p.Value1, p.Value2 })
.Union(db.Table2.Select(q => new { q.Value1, q.Value2 })
.Union(db.Table3.Select(r => new { Value1 = r.Alias1, Value2 = r.Other });
Here the anonymous type serves as a common type to project all three tables onto, so that Union can work.
Related
I want to make this query in linq to sql .
Please help. I am new to linq and having problem to with the group by clause .
Here is the sql query
select count(USERID), d.DEPTNAME from USERS u
join department d on u.DEPTID = d.DEPTID
group by u.DEPTID, d.DEPTNAME
A more direct translation would be like this:
var query =
from u in db.Users
join d in db.Departments on u.DeptId equals d.DeptId
group d by new { d.DeptId, d.DeptName } into g
select new
{
g.Key.DeptName,
Count = g.Count(),
};
Though I think it would be better off written like this:
// looks like we're counting users in each department
var query =
from d in db.Departments
select new
{
d.DeptName,
Count = db.Users.Count(u => u.DeptId == d.DeptId),
};
In linq i am trying to do like this
select * from tbl1 join tbl2 on tbl1.column1= tbl2.column1 and tbl1.column2 = tbl2.column2
how can i write the above query in Linq.... i tried like this but giving error
var sasi = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable()
on new {
table1.Field<string >["SAPQuotationNo"],
table1.Field<string >["Invoiceno"]}
equals new {
table2.Field<string>["SAPQuotationNo"],
table2.Field <string>["Invoiceno"]
}
Use anonymous types
give the properties names
select something
use DataRow.Field as method with round brackets
var sasi = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable()
on new
{
SAPQuotationNo = table1.Field<string>("SAPQuotationN"),
Invoiceno = table1.Field<string>("Invoiceno")
} equals new
{
SAPQuotationNo = table2.Field<string>("SAPQuotationNo"),
Invoiceno = table2.Field<string>("Invoiceno")
}
select table1;
You can try something like this:
from A in context.A
join B in context.B on new { id = B.ID,//..., type = A.ID,//...}
This is the hint, you can explore.
I cant get the fallowing request working in Mono:
Without joins it would work.
when I only select t1 it also works, but I cant select something from both tables.
I think I want a left join, where I always have entries in t1 and IF the NameOfFile matches FileName then I want to have the tables joined.
Extra Question: When is my query executed? When I run the foreach loop?
var result = (
from t1 in db.Table1
join t2 in db.Table2 on t1.FileName equals t2.NameOfFile
into joinDep
from t3 in joinDep.DefaultIfEmpty ()
select new
{
Time = t1.WriteTime,
Name = t2.NameOfFile
}
)
.OrderByDescending (c => c.Time.Date)
.Take (10);
foreach (var entry in result)
{
Console.WriteLine (entry.Name );
}
Use this:
var query = from t1 in db.Table1
join t2 in db.Table2 on t1.FileName equals t2.NameOfFile into gj
from joinDep in gj.DefaultIfEmpty ()
select new
{
Time = t1.WriteTime,
Name = joinDep.NameOfFile
};
var result = query.OrderByDescending (c => c.Time.Date)
.Take (10);
Yes. Take uses deferred execution.
How to translate query like "select 1, 2" (i.e. without FROM clause) to LINQ statement?
Thanks!
I need to get permissions for a set of user groups. In SQL it looks like
SELECT *
FROM Permission p
INNER JOIN (SELECT GroupID
FROM [Group]
UNION ALL
SELECT 555) AS g
ON (g.GroupID = p.GroupID)
In my case I need to programmatically add a certain code instead "555". I wouldn't like to write special SQL function for that.
I guess you just want to create an anonymous type
var anonymous = new { Column1 = 1, Column2 = 2 };
Edit - Based on Comments
Depending on what your Select projection is you could do something simple like this:
If it is a Int:
var query = (from per in context.permissions
select per).AsEnumerable()
.Concat( new int[] { 1, 2 });
If it is a 'Class'
var query = (from per in context.permissions
select per).AsEnumerable()
.Concat(new CustomClass[]
{
new CustomClass()
{
Prop1= 1
},
}
);
You could also change .Concat to .Union
Why do you need this to be linq?
var numbers = new int[] { 1, 2 };
I suppose
var numbers = Enumerable.Range(1,2);
How can I write a linq to entities query that includes a having clause?
For example:
SELECT State.Name, Count(*) FROM State
INNER JOIN StateOwner ON State.StateID = StateOwner.StateID
GROUP BY State.StateID
HAVING Count(*) > 1
Any reason not to just use a where clause on the result?
var query = from state in states
join stateowner in stateowners
on state.stateid equals stateowner.stateid
group state.Name by state.stateid into grouped
where grouped.Count() > 1
select new { Name = grouped.Key, grouped.Count() };
I believe you can use a GroupBy followed by a Where clause and it will translate it as a Having. Not entirely sure though.
If you want to compare a variable that is not in the group by (Ex: age), then it would be:
var duplicated = (
from q1 in db.table1
where (q1.age >= 10 )
group q1 by new { q1.firstName, q1.lastName } into grp
where (grp.Count() > 1 )
select new
{
firstName= grp.Key.firstName,
lastName = grp.Key.lastName,
}
);