This question already has answers here:
Adding multiple parameterized variables to a database in c#
(2 answers)
Parameterize an SQL IN clause
(41 answers)
Closed 7 days ago.
I am trying to pass array of parameter to SQL query as a parameterized query.
SELECT D.PATIENTID AS PATIENTID,
D.FNAME AS FIRSTTNAME
FROM DETAILS D INNER JOIN PATEINT P ON P.PATIENTID=D.PATIENTID
WHERE P.COURSENAME IN ('PA-PA','PA-RO','RO-PA') AND D.STARTDATE BETWEEN '20241201' AND '20241231'
So above query I want to pass as
SELECT D.PATIENTID AS PATIENTID,
D.FNAME AS FIRSTTNAME
FROM DETAILS D INNER JOIN PATEINT P ON P.PATIENTID=D.PATIENTID
WHERE P.COURSENAME IN (#param1,#param2,#param3) AND D.STARTDATE BETWEEN #AntiParam1 AND #AnitParam2
(#param1,#param2,#param3) will be #AnitParam3
Tried below method:
List<SqlParameter> params = new List<SqlParameter>();
foreach (var o in strPackageCode)
{
SqlParameter paramRef = new SqlParameter();
paramRef.ParameterName = "#Param" + params.Count;
paramRef.Value = o;
params.Add(paramRef);
}
And called ExecuteDataSet(Query, parameters); but it takes #AnitParam2 as 'System.Collections.Generic.List`1[System.Data.SqlClient.SqlParameter]'
#AnitParam0=N'20241201',#AnitParam1=N'20241231',#AnitParam2=N'System.Collections.Generic.List`1[System.Data.SqlClient.SqlParameter]'
Related
This question already has answers here:
The type of one of the expressions in the join clause is incorrect in Entity Framework
(4 answers)
Closed 8 years ago.
I'm trying to do this search below, but I'm receiving an error.
ViewBag.IDCONCESSAO =
from p in db.SINCO_CONCESSAO.ToList()
join c in db.MUNICIPIOS_VIEW.ToList() on p.IDMUNICIPIO equals c.NOME_MUNICIPIO
select new
{
Id = p.IDCONCESSAO,
Nome = p.IDCONCESSAO + " - " + c.NOME_MUNICIPIO
};
Error:
The type of one of the expressions in the join clause is incorrect.
Type inference failed in the call to 'Join'.
What is incorrect?
The error was in that line:
join c in db.MUNICIPIOS_VIEW.ToList() on p.IDMUNICIPIO equals c.NOME_MUNICIPIO
I was doing a relation with different types, because IDMUNICIPIO is int and NOME_MUNICIPIO is string. When I changed to this, it worked:
join c in db.MUNICIPIOS_VIEW.ToList() on p.IDMUNICIPIO equals c.ID_MUNICIPIO
This question already has an answer here:
SQL "WHERE IN" query conversion to LINQ
(1 answer)
Closed 8 years ago.
I am trying to query the db with an IN clause using EF:
List<int> ids = new List<int> {1,2,3,4....20};
string sql = GetSql(ids);
//sql is "SELECT * FROM Student WHERE Id in (#p0, #p1, #p2 ... #p19)"
var res = db.Set<Student>().SqlQuery(sql, ids);
But I get the following exception :
No mapping exists from object type System.Collections.Generic.List`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.
I am handling in clause like this:
List<int> ids = new List<int> {1,2,3};
db.Set<Student>().Where(r => ids.Contains(r.ID));
UPDATE:
You do as follows (I have not tested by myself but it should do your job)
public List<CustomObject> ExecuteCustomQuery(List<int> items)
{
var ids = string.Join(",", items.Select(i => i.ToString()).ToArray());
string query = "SELECT Column1, Column1 FROM TABLE1 where id in (" + ids + ")";
return dbContext.Database.SqlQuery<CustomObject>(query).ToList();
}
CustomObject has got two properties of returning columns of select statement.
Let me know how did you go.
thanks.
This question already has answers here:
How to pass an array into a SQL Server stored procedure
(13 answers)
Closed 8 years ago.
I have a list List<Int32> containing ids and want to select some values from another table where id=ids[0],ids[1],...
it look like this:
string query=String.Format("# SELECT values from Table WHERE id=???");
How to get result?
P.S. as i listen- that this way is not right.
So, another way to do that- use Join:
string queryString = String.Format(#" SELECT * FROM Table1 [t1]
join [Table2] [t2]
on [t1].idTable1=[t2].id where [idParamValue]={0}", idParamValue);
So, then i should use :
using (var sqlCmd = new SqlCommand(queryString, _connection))
{
using (var sqlReader = sqlCmd.ExecuteReader())
{
while (sqlReader.Read())
{
var param1=(String)sqlReader["param_name"];
}
}
}
Thank you!
SELECT * FROM Table WHERE id IN (79,86,42)
You can use below query :-
string query=String.Format("# SELECT values from Table WHERE id in(value1,value2,value3,value4,...)");
This question already has answers here:
Make a SQL Query to Nhibernate
(2 answers)
Closed 8 years ago.
How do I get this SQL query to LINQ?
SELECT Customer.name
FROM Company INNER JOIN Customer ON Company.CompanyId = Customer.CompanyId
where CompanyId = 2
You could try this one:
var result = from c in Companies
join cu in Customers
on c.CompanyId equals cu.CompanyId
where c.CompanyId==2
select cu.Name
or
var result = db.Companies
.Join(Customers.Where(x==>x.CompanyId==2),
x=>x.CompanyId,
y=>y.CompanyId,
y=>y.Name);
where Companies and Customers refer to you companies and customers respectively.
This question already has answers here:
How to do a subquery in LINQ?
(6 answers)
Closed 9 years ago.
SELECT userid FROM userTable
WHERE userid in (select writeuserid FROM boardTable)
C# LINQ expressions, how to use a query?
I have been using EF4.
userTable, boardTable is connected to the DbContext.
Why not have two different LINQ queries, so that your inner query doesn't execute for each iteration of the outer query.
var query1 = (from t in dbContext.boardTable
select t.writeuserid).ToArray();
var query2 = from r in dbContext.userTable
where query1.Contains(r.userid)
select r.userid;
If your situation is as simple as in the question then you cause join in linq
Assume in here you use Entity FrameWork, so you can use Join to get the result, below is to use the lambda expression:
var result = dbContext.Users.Join(dbContext.Boards,
user => user.UserId,
board => board.WriteUserId,
(u, b) => u.UserId);
why not using join?
var result = (from u in dbcontext.userTable
join u1 in dbcontext.boardTable on u.userid equals u1.writeuserid
select u.userid).FirstOrDefault();
if (result != null)
// do anything else
else
// user not exists