This is a common Error on SO but I seem not to be able to fix the error
The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ITEye.ViewModels.IssueViewModel]'. Any help to troubleshoot will be appreciated
Viewmodel
public class IssueViewModel
{
public int ItemId { get; set; }
public string Name { get; set; }
public string ItemLocation { get; set; }
public string UserName { get; set; }
}
Controller
public ActionResult Issueds()
{
var query = Issue();
return View(query);
}
public IEnumerable Issue()
{
var issued = from item in db.Items
join issues in db.Issues on item.ItemId equals issues.ItemId
join users in db.Staffs on issues.StaffId equals users.StaffId
where issues.StaffId == users.StaffId
select new IssueViewModel()
{
ItemId = item.ItemId,
Name = item.ItemName,
ItemLocation = item.Location.LocName,
UserName = users.StaffName
};
return issued.AsEnumerable();
}
View
#model IEnumerable<ITEye.ViewModels.IssueViewModel>
#foreach (var item in Model) {
<p>
#item.Name
</p>
}
As sure as I was that the code was okay as I had researched wide on this, the problem turned out to be an error in the view. I used #{Html.RenderPartial("Issueds", "Issue");} instead of #{Html.RenderAction("Issueds", "Issue");}
You have not materialised the Entity Framework query before you return it from Issue() so it will be passed to the view as a SQL string - AsEnumerable() will not do anything because it is already an IEnumerable. You need to materialise the query so use something like ToList() instead.
Also change the return type of the method to be generic:
public ActionResult Issueds()
{
var query = Issue();
return View(query);
}
public IEnumerable<IssueViewModel> Issue()
{
var issued = from item in db.Items
join issues in db.Issues on item.ItemId equals issues.ItemId
join users in db.Staffs on issues.StaffId equals users.StaffId
where issues.StaffId == users.StaffId
select new IssueViewModel()
{
ItemId = item.ItemId,
Name = item.ItemName,
ItemLocation = item.Location.LocName,
UserName = users.StaffName
};
return issued.ToList();
}
Your Issue method should return an IEnumerable<ITEye.ViewModels.IssueViewModel> not the non-generic type IEnumerable
public IEnumerable<ITEye.ViewModels.IssueViewModel> Issue()
{
var issued = from item in db.Items
join issues in db.Issues on item.ItemId equals issues.ItemId
join users in db.Staffs on issues.StaffId equals users.StaffId
where issues.StaffId == users.StaffId
select new IssueViewModel()
{
ItemId = item.ItemId,
Name = item.ItemName,
ItemLocation = item.Location.LocName,
UserName = users.StaffName
};
return issued;
}
Related
I need to translate the following SQLite query to LINQ in C#
SELECT sup.SupplierName, sup.SupplierID, pr.Price, max(pr.AddedDate)
FROM Suppliers sup
LEFT JOIN ItemsPrices pr
USING(SupplierID)
WHERE pr.ItemID = '22'
GROUP BY sup.SupplierName
I've searched about all web site and tried the following LINQ query and it does group like what I want but doesn't select latest date. I'm newbie in LINQ please help me
internal List<LatestPriceDbo> GetLatestPrice(int _itemid)
{
using (dbContext context = new dbContext())
{
var groupedPrice = from a in context.ItemsPrices
where a.ItemId == _itemid
orderby a.Id descending
group a by new { a.ItemId, a.SupplierId } into g
select new ItemsPrice
{
SupplierId = g.Key.SupplierId,
ItemId = g.Key.ItemId,
Price = g.FirstOrDefault().Price,
AddedDate = g.Max(s => s.AddedDate)
};
var result = (from c in context.Suppliers
from k in groupedPrice
where k.ItemId == _itemid && c.SupplierId == k.SupplierId
select new LatestPriceDbo
{
supid = c.SupplierId,
supname = c.SupplierName,
price = k.Price,
addeddate = k.AddedDate
}).ToList();
return result;
}
}
internal class LatestPriceDbo
{
public int supid { get; set; }
public string supname { get; set; }
public decimal price { get; set; }
public string addeddate { get; set; }
}
I am using Database-First.
You should be able to use a LINQ Join I have mocked up something which might point you in the correct direction:
Notes
use the join first to get the set you looking for
you can then do a nested select for the max based on supplierId.
from a in context.ItemsPrices
join s in context.Suppliers on a.SupplierId equals s.supplierId
where a.ItemId == _itemid
orderby a.Id descending
select new ItemsPrice
{
SupplierName = s.SupplierName
SupplierId = a.SupplierId,
ItemId = a.ItemId,
Price = a.FirstOrDefault().Price,
AddedDate = context.ItemsPrices.Where(x => x.SupplierId == a.SupplierId).Max(s => s.AddedDate)
};
I solved the problem owing to Kevin's suggestion. I did need to be more search on web to improve the code block of Kevin's comment and I did.
internal List<LatestPriceDbo> GetLatestPrice(int _itemid)
{
using (dbContext context = new dbContext())
{
var result = (from a in context.ItemsPrices
join s in context.Suppliers on a.SupplierId equals s.SupplierId
where a.ItemId == _itemid
orderby a.Id descending
group new { a, s } by new { a.SupplierId, a.ItemId } into grb
select new LatestPriceDbo
{
supname = grb.FirstOrDefault().s.SupplierName,
supid = grb.Key.SupplierId,
itemid = grb.Key.ItemId,
price = context.ItemsPrices
.Where(x => x.ItemId == grb.FirstOrDefault().a.ItemId)
.OrderByDescending(z => z.Id).Select(z => z.Price)
.FirstOrDefault(),
addeddate = context.ItemsPrices
.Where(x => x.SupplierId == grb.FirstOrDefault().a.SupplierId)
.Max(s => s.AddedDate)
}).ToList();
return result;
}
}
internal class LatestPriceDbo
{
public int itemid { get; set; }
public int supid { get; set; }
public string supname { get; set; }
public decimal price { get; set; }
public string addeddate { get; set; }
public int recordid { get; set; }
}
Hi i convert sql query to linq i got this error. when i remove group by there isn't exception but i should use group by. why i got this exception ?
public List<BiontechSinovacCovidDto> GetBiontechSinovacCovidDto()
{
using(SirketDBContext context=new SirketDBContext())
{
var result =
from asi in context.Asilar
join covid in context.Covids
on asi.CovidId equals covid.CovidId
group asi by asi.AsiIsmi into isim
select new BiontechSinovacCovidDto
{
AsiIsmi=isim.Key,
//exception OrtalamaCovidSuresi=(EF.Functions.DateDiffDay(covid.CovidYakalanmaTarih, covid.CovidBitisTarih)).Average()
};
return result.ToList();
}
}
my sql query
Select
AsiIsmi,
AVG(Cast(DATEDIFF(Day,CovidYakalanmaTarih, CovidBitisTarih)AS FLOAT)) as OrtalamaCovidSuresi
From Asilar
INNER JOIN Covids on Covids.CovidId=Asilar.CovidId
group by AsiIsmi
|AsiIsmi| OrtalamaCovidSuresi|
------- ------------------
Biontech 13.6667
Sinovac 15
BiontechSinovacCovidDto
public class BiontechSinovacCovidDto
{
public string AsiIsmi { get; set; }
public double OrtalamaCovidSuresi { get; set; }
}
Average has overload which accepts lambda. You have to use this version. Also you have to group covid in this case.
public List<BiontechSinovacCovidDto> GetBiontechSinovacCovidDto()
{
using (var context = new SirketDBContext())
{
var result =
from asi in context.Asilar
join covid in context.Covids
on asi.CovidId equals covid.CovidId
group covid by asi.AsiIsmi into isim
select new BiontechSinovacCovidDto
{
AsiIsmi = isim.Key,
OrtalamaCovidSuresi = isim.Average(x => EF.Functions.DateDiffDay(x.CovidYakalanmaTarih, x.CovidBitisTarih))
};
return result.ToList();
}
}
I checked the questions that may already have my answer but it seems that none of them addresses the issue I have:
var semesters = db.Semesters.Where(e => e.ID == semester).ToList();
var semestersbyfaculty = db.SemestersByFaculty.Where(e => e.FacultyID == id).ToList();
How do I inner join these two queries like if I do the following in SQL:
SELECT
Fac.*, Sem.SemesterText
FROM
SemestersByFaculty AS Fac
INNER JOIN
Semesters AS Sem
ON
Fac.SemesterID = Sem.ID
WHERE id
Fac.FacultyID = id
Inner Join
The following example shows a simple inner equijoin.
var query = from fac in db.SemesterByFaculty
join sem in db.Semester on fac.SemesterID equals sem.ID
where fac.FacultyID == id
select new { Faculty = fac, SemesterText = sem.SemesterText };
For more information, see How to: Perform Inner Joins (C# Programming Guide).
UPDATE:
from comments
Models
class MyModel {
public MitModel.SemestersByFaculty Faculty{ get; set; }
public string SemesterText { get; set; }
}
class MyViewModel {
public List<MyModel> SemesterFaculties { get; set; }
}
Action:
public ActionResult SomeAction(string id) {
var query = from fac in db.SemesterByFaculty
join sem in db.Semester on fac.SemesterID equals sem.ID
where fac.FacultyID == id
select new MyModel{ Faculty = fac, SemesterTest = sem.SemesterTest };
var viewModel = new MyViewModel { SemesterFaculties = query.ToList() };
return View(viewModel);
}
View
#Model MyViewModel
Suppose you have following fields in SemestersByFaculty
class SemestersByFaculty
{
string FacultyName;
int FacultyID;
int SemesterID;
}
Note : If you have more fields in SemestersByFaculty class then you can list them in new{} in query below:
var query = semesters.Join(semestersbyfaculty,
sem => sem.ID,
fac => fac.SemesterID,
(sem, fac) =>
new { facName = fac.FacultyName, facId = fac.FacultyID,semText = sem.SemesterText }).Where(e=> e.fac.FacultyId = id);
Now , what you have got yourself is an enumerable. You can iterate over it and retrieve the values. It will be like:
foreach (var obj in query){
Console.writeln("{0}-{1}-{2}",obj.facName,obj.facId,obj.semText);
}
Here is solution with non-LINQ syntax. Also you can perform 'Where' filter on 'db.Semesters' and 'db.SemestersByFaculty' before they are passed into 'join'.
var result = db.SemestersByFaculty
.Where(
x_ => x_.FacultyID == id)
.Join(
db.Semesters.Where(x_ => x_.ID == semester),
x_ => x_.SemesterID,
x_ => x_.ID,
(x_, y_) => new
{
FacultyID = x_.ID,
SemesterID = y_.SemesterID,
Sem = y_.SemesterText
})
.ToList();
I am trying to implement Inner join query on two tables opportunityProducts and Products where I am supposed to return Iqueryable element in my MVC web API service. But from below, I am not able to get result as it throws error for conversion.
public IQueryable<OpportunityProducts> GetProductsByShipID(int id)
{
IQueryable<OpportunityProducts> oppProductss =
from c in db.OpportunityProducts
from p in db.Products
where p.ProductID == c.ProductID
select new { c.Quantity,c.ProductDesc,c.RemainingQuantity, p.QtyInHand};
return oppProductss;
}
You need to fill the Type you wish to return instead of returning an anonymous type. Here since you are querying the OpportunityProducts, I think you don't have QtyInHand property. So you can either return a new type altogether or add this property.:-
IQueryable<ResultantProducts> oppProductss =
from c in db.OpportunityProducts
from p in db.Products
where p.ProductID == c.ProductID
select new ResultantProducts
{
Quantity = c.Quantity,
ProductDesc = c.ProductDesc,
RemainingQuantity = c.RemainingQuantity,
QtyInHand = p.QtyInHand
};
I see an error in your code. You should return objects of type OpportunityProducts, I mean:
public IQueryable<OpportunityProducts> GetProductsByShipID(int id)
{
IQueryable<OpportunityProducts> oppProductss = from c in db.OpportunityProducts
from p in db.Products
where p.ProductID == c.ProductID
select new OpportunityProducts // <---- THIS!
{
Quantity = c.Quantity,
ProductDesc = c.ProductDesc,
RemainingQuantity = c.RemainingQuantity,
QtyInHand = p.QtyInHand
};
return oppProductss;
}
I hope it helps you.
Regards,
Julio
I think you can create class called ResultProducts with all properties(same data type in the original table (nullable also need to put)) what you want to get. Then you can return that object.
public class ResultProducts
{
public int Quantity { get; set; }
public string ProductDesc { get; set; }
public int RemainingQuantity { get; set; }
public int QtyInHand { get; set; }
}
public IQueryable<ResultProducts> GetProductsByShipID(int id)
{
var oppProductss =from c in db.OpportunityProducts
from p in db.Products
where p.ProductID == c.ProductID
select new ResultProducts()
{
Quantity =c.Quantity,
ProductDesc= c.ProductDesc,
RemainingQuantity=c.RemainingQuantity,
QtyInHand=p.QtyInHand
};
return oppProductss ;
}
I hope this will work.
I keep getting the error below on my code, and can't understand why it is having problems translating it to a query, it is pretty simple.
I have 2 repositories, Album and AlbumImage, when I fetch an album do I want a cover, that is a subselect in AlbumImages. What am I doing wrong here?
LINQ to Entities does not recognize the method
'System.Linq.IQueryable`1[Sogaard.us.Cosplay.Data.AlbumImage] Get()'
method, and this method cannot be translated into a store expression.
Album repository
public class AlbumRepository : IRepository<Album>
{
private CosplayEntities _entities;
private IRepository<AlbumImage> _imageRepository;
public AlbumRepository(CosplayEntities entities, IRepository<AlbumImage> imageRepository)
{
_entities = entities;
_imageRepository = imageRepository;
}
public IQueryable<Album> Get()
{
return (from a in _entities.Albums
select new Album()
{
Id = a.Id,
UserId = a.UserId,
Name = a.Name,
Created = a.Created,
LastEdit = a.LastEdit,
Description = a.Description,
Views = a.Views,
Location = a.Location,
Photoshoot = a.Photoshoot,
Cover = (from ai in _imageRepository.Get() where ai.AlbumId == a.Id orderby ai.Cover descending, ai.Id ascending select ai).FirstOrDefault(),
});
}
}
AlbumImage repository
public class AlbumImageRepository : IRepository<AlbumImage>
{
private CosplayEntities _entities;
public AlbumImageRepository(CosplayEntities entities)
{
_entities = entities;
}
public IQueryable<AlbumImage> Get()
{
return (from ai in _entities.AlbumImages
select new AlbumImage()
{
Id = ai.Id,
AlbumId = ai.AlbumId,
UserId = ai.UserId,
Type = ai.Type,
Width = ai.Width,
Height = ai.Height,
Description = ai.Description,
Views = ai.Views,
Uploadet = ai.Uploadet,
LastView = ai.LastView,
Thumblink = ai.Thumblink,
Imagelink = ai.Imagelink,
Cover = ai.Cover
});
}
This is the code i am getting the error on
_albumImageRepository = new AlbumImageRepository(_entities);
_albumRepository = new AlbumRepository(_entities, _albumImageRepository);
_albumImagesTagRepository = new AlbumImagesTagRepository(_entities);
....
var album = _albumRepository.Get().Where(x => x.Id == image.AlbumId).FirstOrDefault();
Update: I have commented the Cover = ... out in my IQueryable Get() so it is 2 simple select as object.
And i still get the error in something as simple as
model.Albums = (from a in _albumRepository.Get()
orderby a.Id descending
select new AlbumDisplayModel()
{
Album = a,
ImageCount = _albumImageRepository.Get().Where(x => x.AlbumId == a.Id).Count(),
User = _userRepository.Get().Where(x => x.Id == a.UserId).FirstOrDefault()
})
.Skip(AlbumsPrPage * (page - 1))
.Take(AlbumsPrPage).ToList();
Update 2: If i rewrite the IQueryable Get() to the following, do it work flawlessly, there there should really be no diffrence in how it is handled?
public IQueryable<Album> Get()
{
return (from a in _entities.Albums
select new Album()
{
Id = a.Id,
UserId = a.UserId,
Name = a.Name,
Created = a.Created,
LastEdit = a.LastEdit,
Description = a.Description,
Views = a.Views,
Location = a.Location,
Photoshoot = a.Photoshoot,
Cover = (from ai in _entities.AlbumImages where ai.AlbumId == a.Id orderby ai.Cover descending, ai.Id ascending select new AlbumImage()
{
Id = ai.Id,
AlbumId = ai.AlbumId,
UserId = ai.UserId,
Type = ai.Type,
Width = ai.Width,
Height = ai.Height,
Description = ai.Description,
Views = ai.Views,
Uploadet = ai.Uploadet,
LastView = ai.LastView,
Thumblink = ai.Thumblink,
Imagelink = ai.Imagelink,
Cover = ai.Cover
}).FirstOrDefault(),
});
}
Update 3: Did a little test, and the problem seems to be with Entity framework, se the following code, The var linqAlbum = testClass.LinqAlbumGet().ToList(); executes without any problems and return the correct data, var eeAlbum = testClass.EEAlbumGet().ToList(); fails with the exception
LINQ to Entities does not recognize the method
'System.Linq.IQueryable`1[RepositoryTest.TestAlbumCover] EEImageGet()'
method, and this method cannot be translated into a store expression.
My test script
class Program
{
static void Main(string[] args)
{
var linq = new LinqDataContext();
var ee = new NewCosplayEntities();
var testClass = new Test(linq, ee);
var linqAlbum = testClass.LinqAlbumGet().ToList();
var eeAlbum = testClass.EEAlbumGet().ToList();
}
}
public class Test
{
public NewCosplayEntities ee { get; set; }
public LinqDataContext linq { get; set; }
public Test(LinqDataContext linq, NewCosplayEntities ee)
{
this.linq = linq;
this.ee = ee;
}
public IQueryable<TestAlbum> LinqAlbumGet()
{
return from a in linq.Albums
select new TestAlbum
{
Id = a.Id,
Name = a.Name,
Cover = (from i in LinqImageGet() where i.AlbumId == a.Id select i).FirstOrDefault()
};
}
public IQueryable<TestAlbumCover> LinqImageGet()
{
return from i in linq.AlbumImages
select new TestAlbumCover()
{
Id = i.Id,
AlbumId = i.AlbumId
};
}
public IQueryable<TestAlbum> EEAlbumGet()
{
return from a in ee.Albums
select new TestAlbum
{
Id = a.Id,
Name = a.Name,
Cover = (from i in EEImageGet() where i.AlbumId == a.Id select i).FirstOrDefault()
};
}
public IQueryable<TestAlbumCover> EEImageGet()
{
return from i in ee.AlbumImages
select new TestAlbumCover()
{
Id = i.Id,
AlbumId = i.AlbumId
};
}
}
public class TestAlbum
{
public int Id { get; set; }
public string Name { get; set; }
public TestAlbumCover Cover { get; set; }
}
public class TestAlbumCover
{
public int Id { get; set; }
public int AlbumId { get; set; }
}
Your problem comes in the ItemRepository for Albumn. Specifically because _entities has no knowledge of the _imageRepository type, so it doesn't know how to translate that type into the appropriate TSQL script. You could cast the _entities.Albums.ToList() to force the IQueryable into an IEnumerable before you try to access the _ImageRepository.Get() from the scope of the hydrated object instead of directly on the database instance. Realize that you are then going to see a perf hit on the n+1 database requests for the AlbumImage child objects for each Album.
public IQueryable<Album> Get()
{
return (from a in _entities.Albums
select new Album()
{
Id = a.Id,
UserId = a.UserId,
Name = a.Name,
Created = a.Created,
LastEdit = a.LastEdit,
Description = a.Description,
Views = a.Views,
Location = a.Location,
Photoshoot = a.Photoshoot,
Cover = (from ai in _imageRepository.Get() where ai.AlbumId == a.Id orderby ai.Cover descending, ai.Id ascending select ai).FirstOrDefault(),
});
}
Ultimately, the problem is that your trying to use an ActiveRecord pattern rather than a true repository. Everything in a single IQueryable needs to be fetched through the same database context instance for parsing and tracking purposes.
Potentially its because you are wrapping the Album and AlbumImage in new references. I would remove that and do the projection after your query.
I don't think you can project into an entity and have each projection use a result from another IQueryable. If you replaced the contents of IQueryable<AlbumImage> Get() with this, it might work:
from a in _entities.Albums
join c in _imageRepository.Get() on a.Id equals c.AlbumId into acJoin
from ac in acJoin.DefaultIfEmpty()
select new Album()
{
Id = a.Id,
etc..,
etc..,
Cover = ac
}
I'm actually fairly certain that you will need to adjust this freehand query, but essentially it's joining the IQueryables, then projecting those results into your objects, instead of projecting to your objects then inserting an IQueryable into those results. Not the best explanation I know, but just look up "LINQ Left Join" or "Linq Left Outer Join" to see the syntax of what I'm describing here. Example