I want to do this in linq:
SQL: select from customer where city in ('Berlin','London','Madrid','Bern',Graz')
So I have a string like shown below:
string city ="'Berlin','London','Madrid','Bern','Graz'"
var query = from c in Customers
where c.City ==??
select c;
The string variable will change...
Thanks a lot.
Ok I found out...
string[] city2 = { "Berlin", "London", "Madrid", "Bern", "Graz" };
var query2 = from c in Customers
where city2.Contains(c.City)
select c;
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 want to join the following Tables
1. B_Book[1st Table]
-B_BID (Book ID)(PK)
-B_Name
-B_CategroyID (FK)
2. BI_BookInstance [2nd Table]
-BI_IID(Instance ID)
-BI_BID (FK)
-BI_Price
3. BC_BookCategory [3rd Table]
-BC_CategoryID (PK)
-BC_CategoryName
First Join B_Book and BI_BookInstance then join the result of those both with BookCategory.
(1st join)[B_BID equals BI_BID]
(2nd nested join)[result of 1st join B_CategoryID equals BC_CategoryID]
Edit
SQL would be something like the following:
SELECT * FROM
(SELECT * FROM B_Book b JOIN BI_BookInstance bi on b.B_BID = bi.BI_BID) as t1
JOIN BC_BookCategoryID bc on bc.BC_CategoryID = t1.B_CategoryID
What matches your query in LINQ would be the following (and you'll notice the similarity with SQL). I've also included some examples on how to rename the fields returned, such as Price or CategoryName:
var results = from b in B_Book
join bi in BI_BookInstance
on b.B_BID equals bi.BI_BID
join bc in BC_BookCategory
on b.B_CategoryID equals bc.BC_CategoryID
select new
{
// put in whatever fields you want returned here:
b.B_BID,
b.B_CategoryID,
b.B_Name,
bi.BI_BID,
bi.BI_IID,
Price = bi.BI_Price,
bc.BC_CategoryID,
CategoryName = bc.BC_CategoryName
};
I have supposed inner joins (your FKs is not null), so i would like query like this:
var ctx = new YourEntities();
var query = from b in ctx.B_Book
from bi in ctx.BI_BookInstance
from bc in ctx.BC_BookCategory
where b.B_BID == bi.BI_BID && b.B_CategoryID == bc.BC_CategoryID
select new
{
BInstID = bi.BI_IID,
BName = b.B_Name,
BPrice = bi.BI_Price,
BCategory = bc.BC_CategoryName
};
foreach (var item in query)
{
Console.WriteLine(item.BInstID);
Console.WriteLine(item.BName);
Console.WriteLine(item.BPrice);
Console.WriteLine(item.BCategory);
Console.WriteLine("");
}
You can do this without explicitly using linq's join statement, provided that navigation properties are in place:
from b in ctx.B_Book
from bi in b.BookInstances
select new { b.Property1, bi.Property2, b.BookCategory.Name }
Say, I have the following tables in SQLCe Database:
Table Country Table Cities
----------- -------------
Pkey countryname Pkey Fkey cityname
1. USA 1. 1. LA
2. Canada 2. 2. Toronto
3. 1. NYC
4. 1. Chicago
Here the code to get the cities:
public IList GetCities(){
IList cityList = null;
using (CountryDataContext context = new CountryDataContext(ConnectionString))
{
IQueryable query = from c in context.Cities select c;
cityList = query.ToList();
}
return cityList;
}
Questions are :
How do I select using normal Select-statement : Select cityname where country = USA?
How do I select the country and get the primary key ?
Select ID, Countryname
then, I can pass the Pkey and do a select again
Select cityname where Fkey = "1";
would apreciate any help or sample on LinqToSQL for normal Databse operation.
Thanks
This should get you your first question:
var query = from c in context.Cities
join y in context.Country
on c.Pkey equals y.Fkey
where y.countryname.Equals("USA")
select c.cityname;
I'm not sure I understand what you're asking in the second question, but I think this may be it:
var query = from c in context.Cities
join y in context.Country
on p.Pkey equals y.Fkey
where y.Pkey.Equals(1)
select new
{
c.Pkey,
c.cityname
};
I have a collection that I'm gathering from a linq query and I'd like to use that collection as a join for another query. How do I do that?
Thanks
LINQ 101 Samples
Copied LINQ statements from here
string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" };
List<Product> products = GetProductList();
var q =
from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName };
foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
Try this
var query2 = from c in db.YourTable
join q in query1 on c.Id equals q.Id
select c;
Where query1 wats your first collection that originated from a linq query.
You should be able to do this:
List<Product> products = GetProductList(); //collection
var q = from c in categories
join p in products on c equals p.Category into ps
select new { Category = c, Products = ps };
Below is a Linq query I wrote that does exactly that. This is a Linq to SQL query that joins in a local collection.
int[] keyList = new int[] { 7064, 7065, 6242 };
var query = (from child in ETAModule
join parent in ETA on child.ExpID equals parent.ExpID
where child.ID >= 65490442
where keyList.Contains(child.ExpID)
orderby child.ID
select new { EtaModule = child, Eta = parent });
...not a whole lot of information provided, but here is a very simple example.
var firstCollection = List<YourClassNameHere>(); // this is your pre-existing collection
var results = from o in firstCollection
join i in SomeOtherCollection on o.JoinField equals i.JoinField
select i; // or whatever you like