Please help. I am new in ASP.NET MVC and I am trying to send a query to a repository but it gives me an error:
Errr 3 Cannot implicitly convert type
'System.Collections.Generic.List' to
'System.Collections.Generic.IList'. An
explicit conversion exists (are you missing a cast?)
I am using a a Schema Class with only the columns that I need. This is the code that I am using for the repository.
public class SSGridRepository : SSGridIRepository
{
private DataClassesSSDataContext db;
public SSGridRepository()
{
db = new DataClassesSSDataContext();
}
public IList<SSQuerySchema> ListAll()
{
var SSQuery = (from HISTORies in db.HISTORies
join SSes in db.SSes on HISTORies.WO equals SSes.WO
join SSCUSTOMs in db.SSCUSTOMs on SSes.WO equals SSCUSTOMs.WO
join StatusTables in db.StatusTables on new { STATUS = SSes.STATUS } equals new { STATUS = StatusTables.Status }
join StatusTable_1 in db.StatusTables on new { OLDSTATUS = HISTORies.OLDSTATUS } equals new { OLDSTATUS = StatusTable_1.Status }
join StatusTable_2 in db.StatusTables on new { NEWSTATUS = HISTORies.NEWSTATUS } equals new { NEWSTATUS = StatusTable_2.Status }
where
HISTORies.OLDSTATUS == "m" &&
HISTORies.NEWSTATUS == "n" &&
HISTORies.ACTION == "Change Job Status" &&
HISTORies.OLDSTATUS != HISTORies.NEWSTATUS &&
HISTORies.DATE.Value.Year == Convert.ToDateTime(DateTime.Now).Year ||
HISTORies.OLDSTATUS != HISTORies.NEWSTATUS &&
HISTORies.NEWSTATUS == "m" &&
HISTORies.ACTION == "Checked In Work Order" &&
HISTORies.DATE.Value.Year == Convert.ToDateTime(DateTime.Now).Year
orderby
HISTORies.DATE
select new
{
HISTORies.WO,
SSes.TITLE,
SSes.DESCRIPT,
SSCUSTOMs.CUSTNAME,
SSes.STAKER,
HISTORies.USER,
SSes.STATUS,
HISTORies.OLDSTATUS,
HISTORies.NEWSTATUS,
CURRENT_STATUS = StatusTables.Description,
OLD_STATUS = StatusTable_1.Description,
NEW_STATUS = StatusTable_2.Description,
HISTORies.DATE.Value.Month,
HISTORies.DATE
}).Distinct();
return SSQuery.ToList();
}
}
In you Linq you do this:
select new {
HISTORies.WO,
SSes.TITLE,
SSes.DESCRIPT,
SSCUSTOMs.CUSTNAME,
SSes.STAKER,
HISTORies.USER,
SSes.STATUS,
HISTORies.OLDSTATUS,
HISTORies.NEWSTATUS,
CURRENT_STATUS = StatusTables.Description,
OLD_STATUS = StatusTable_1.Description,
NEW_STATUS = StatusTable_2.Description,
HISTORies.DATE.Value.Month,
HISTORies.DATE
}
Which is a dynamic type so it wont match you return type of List<SSQuerySchema>
Try initializing the type you have specified in your Linq and set the properties of course.
select new SSQuerySchema {
// initialize all properties here
}
You're trying to return an IList<SSQuerySchema> but your actual return type is IList<dynamic> (Your not selecting SSQuerySchema's but an anonymous type.
You should either make the return type of the function IList<dynamic> or modify your select to create new instances of SSQuerySchema
Related
I am trying to use linq2db.EntityFrameworkCore for some of its windowing functions, such as RANK().
Below is my implementation:
var abc = (from tl in _repo.Context.TransferLink
join tlt in _repo.Context.TransferLinkType on new { TLinkId = tl.TransferLinkTypeId, EType = "Deviance" } equals new { TLinkId = tlt.TransferLinkTypeId, EType = tlt.EnumTransferLinkType }
//let duplicateCount = _repo.Context.TransferLink.Where(tl1 => tl1.SecondaryTransferId != null && tl.SecondaryTransferId != null &&
//tl1.SecondaryTransferId == tl.SecondaryTransferId.Value).Count()
where
(allTransferIds.Contains(tl.PrimaryTransferId) || allTransferIds.Contains(tl.SecondaryTransferId)) &&
!tl.Archived
select new
{
TransferLinkId = tl.TransferLinkId,
TransferLinktypeId = tl.TransferLinkTypeId,
PrimaryTransferId = tl.PrimaryTransferId,
SecondaryTransferId = tl.SecondaryTransferId,
DuplicateCount = Sql.Ext.Count(tl.TransferLinkId)
.Over()
.PartitionBy(tl.SecondaryTransferId)
.ToValue()
UpdatedDate = tl.UpdatedDate,
RankVal = Sql.Ext.Rank()
.Over()
.PartitionBy(tl.TransferLinkTypeId, tl.SecondaryTransferId)
.OrderByDesc(tl.UpdatedDate)
.ThenBy(tl.TransferLinkId)
.ToValue()
}).ToList();
This code throws the exception:
Rank is server-side method
I have tried searching for a solution, but could not find any.
Any idea?
For using linq2db.EntityFrameworkCore you have to switch to library's LINQ provider. It can be done by simple ToLinqToDB() call.
var query = /* some EF Core query */
query = query.ToLinqToDB();
var result = query.ToList();
Error:
Convert implicitly system.collections.generic.list return data query
My code:
public List<td_encuestas> getEncPreg(int userId)
{
db.Configuration.LazyLoadingEnabled = false;
var encuesta = (from enc in db.td_encuestas
join pre in db.td_preguntas on enc.enc_id equals pre.pre_enc_id
join res in db.td_respuestas on pre.pre_enc_id equals res.res_id
where enc.enc_activo == "true"
&& pre.pre_activo == "true"
&& enc.enc_usr_id_registro == userId
orderby enc.enc_descripcion
select new
{
enc,
pre,
res
}).ToList();
return encuesta;
}
Return collection and relationship
The Linq procedure that you are using does not return a List of that type/object, you should use a dynamic method, which returns something without knowing what it is, here's the code:
public dynamic List<td_encuestas> getEncPreg(int userId)
{
db.Configuration.LazyLoadingEnabled = false;
var encuesta = (from enc in db.td_encuestas
join pre in db.td_preguntas
on enc.enc_id equals pre.pre_enc_id
join res in db.td_respuestas
on pre.pre_enc_id equals res.res_id
where enc.enc_activo == "true"
&& pre.pre_activo == "true"
&& enc.enc_usr_id_registro == userId
orderby enc.enc_descripcion
select new
{
enc,
pre,
res
}).ToList();
return encuesta;
}
And to use it:
var obj = getEncPreg(someId);
Documentation.
A generic list is not equal to List<td_encuestas>
Please can you tell me how to get a single data object in this method.
It's difficult for me , because of returning type. I'm trying to get a single data from my MongoDB collection, and then use that that on different form to show who is active. My error is always in definition of returning type.
This is my code:
public var PrijavljenKorisnik(ModelPrijavaKorisnika prvKor)
{
MongoCollection<ModelKorisici> kljenti = GetTasksCollection();
List<ModelKorisici> DohvaceniKorisnik = new List<ModelKorisici>();
var upit = from lambda in kljenti.AsQueryable<ModelKorisici >()
where lambda.kor_ime == prvKor.PrijavaKor && lambda.uloga == "korisnik"
select lambda;
foreach (var dohvaceni in upit)
{
DohvaceniKorisnik.Add(dohvaceni);
if (DohvaceniKorisnik.Count() >= 1 && dohvaceni.uloga == "korisnik")
{
MessageBox.Show("Ovaj korinik je prijavljen kao korisnik");
frmKorisnik fmk = new frmKorisnik();
fmk.Show();
frmPrijavaForma frmPriv = new frmPrijavaForma();
frmPriv.Close();
return dohvaceni;
}
else
{
DialogResult d = MessageBox.Show("Potrebno se je predhodno registrirati u aplikaciju KnjigoLjubac");
if (d == DialogResult.Yes)
{
frmRegistracijaForma frmReg = new frmRegistracijaForma();
frmReg.Show();
return null;
}
}
}
I'm not entirely sure what your intent is, but I'll take a stab at it. You suggested that you're looking for a single value, so I thought this might work for you:
var dohvaceni = (from lambda in kljenti.AsQueryable<ModelKorisici >()
where lambda.kor_ime == prvKor.PrijavaKor && lambda.uloga == "korisnik"
select lambda)
.FirstOrDefault();
if (dohvaceni != null)
{
MessageBox.Show("Ovaj korinik je prijavljen kao korisnik");
// the rest of the code for a match...
}
else
{
DialogResult d = MessageBox.Show("Potrebno se je predhodno registrirati u aplikaciju KnjigoLjubac");
// the rest of the code for no matches...
}
I hope this can help.
What does this error mean?
My error is:
Cannot implicitly convert type 'System.Linq.IQueryable<System.Linq.IGrouping<decimal,Project1.Domain.Models.DepartmentBreakdownReport>>' to 'System.Linq.IQueryable<Project1.Domain.Models.DepartmentBreakdownReport>'. An explicit conversion exists (are you missing a cast?)
I would like to know what it is in order for me to be able to fix it. It only happens when I add a LINQ GroupBy onto my results.
public IQueryable<DepartmentBreakdownReport> GetDepartmentBreakdownBySupplierIDAndReviewID(int ClientID, int? SupplierID, int? ReviewID) {
return (from d in Db.Details
join h in Db.Headers
on new { d.ClientID, d.ClaimID }
equals new { h.ClientID, h.ClaimID }
where d.ClientID == ClientID && h.SupplierID == SupplierID
join sd in Db.SuppDepts
on new { a = d.ClientID, b = d.CategoryID ?? 0 }
equals new { a = sd.ClientID, b = sd.CategoryID }
join r in Db.Reviews
on new { h.ClientID, h.ReviewID }
equals new { r.ClientID, r.ReviewID }
join rp in Db.ReviewPeriods
on new { a = r.ClientID, b = r.ReviewPeriodID ?? 0 }
equals new { a = rp.ClientID, b = rp.ReviewPeriodID }
where r.ReviewID == ReviewID
join su in Db.Suppliers
on new { h.ClientID, h.SupplierID }
equals new { su.ClientID, su.SupplierID }
select new DepartmentBreakdownReport {
DepartmentName = sd.DepartmentName,
SumOfAmount = d.Amount,
SupplierID = h.SupplierID,
ReviewID = h.ReviewID,
ReviewName = rp.ReviewPeriodName,
SupplierName = su.SupplierName,
ClientID = d.ClientID
}).GroupBy(r=>r.Amount);
}
What does this error mean?
Your return type is IQueryable<DepartmentBreakdownReport> yet you are attempting to return a IGrouping<decimal,Project1.Domain.Models.DepartmentBreakdownReport>
When you call .GroupBy you change the return type to IGrouping rather than IQueryable, it also introduces the decimal as part of the IGrouping expression (Which is the amount variable you are grouping by).
To fix it you can simply change the method signature to:
public IGrouping<decimal,Project1.Domain.Models.DepartmentBreakdownReport> GetDepartmentBreakdownBySupplierIDAndReviewID(int ClientID, int? SupplierID, int? ReviewID)
To me, the method name GetDepartmentBreakdownBySupplierIDAndReviewID would imply:
Firstly that the method return type would be something like this, assuming types of int for SupplierId and ReviewId :
IGrouping<Tuple<int, int>, IQueryable<DepartmentBreakdownReport>>
GetDepartmentBreakdownBySupplierIDAndReviewID(...)
Secondly that the return should be on a final projection something like:
select new DepartmentBreakdownReport { ... })
.GroupBy(r => new Tuple<int, int>(r.SupplierID, r.ReviewID));
My intersect in LINQ somehow dont seem to work. I have two excel sheets. I fetch it using LinQToExcel and (LinQToExcel does not support VisitSubQueryExpression i have to do some extra work).
List<BoardSheet> sourceTest = (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>();
List<BoardSheet> targetTest = (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>();
IEnumerable<BoardSheet> board = sourceTest.Intersect(targetTest);
board's count always returns 0. But when i iterate thro the field values of sourceTest and targetSet i see common field values.
These are instances of reference types. Intersect is using the DefaultComparer for reference types, which is ReferenceEquals. Since sourceTest has no common instances with targetTest, no results are found.
You could create a Comparer, or you could join like this:
List<CircuitSet> results =
(
from s in sourceTest
join t in targetTest
on s.Id equals t.Id
where s.Data == t.Data && s.ControlType == t.ControlType ...
select s
).ToList();
I think an easy way would be to implement IEqualityComparer<CircuitSet>. If CircuitSet's key is ID, then you could do it like this:
public class CircuitSetComparer : IEqualityComparer<CircuitSet>
{
#region IEqualityComparer<CircuitSet> Members
public bool Equals(CircuitSet x, CircuitSet y)
{
return x.ID == y.ID;
}
public int GetHashCode(CircuitSet obj)
{
return obj.ID;
}
#endregion
}
Then in your code:
IEnumerable<BoardSheet> board = sourceTest.Intersect(targetTest, new CircuitSetComparer());
GetHashCode method is tricky though, but it should be alright if my assumptions (ID being the key) are correct.
but i changed the query based on what david suggested
List<BoardSheet> sourceTest =(from s in (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>() jon tbl in (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>() on s.ID equals tbl.ID select s).ToList<BoardSheet>() ;