Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have two model first is expense and second is Itemmodels. Store all Product in Itemmodels with id,Name and in expense store only itemId. so i need get record from both table through join code is below. But this is fetch error:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression

C#
var data = db.expense
                    .Join(db.Itemmodels, q => q.ITEM, o => o.id, (q, ITEM) => new { q, ITEM })
                    .Where(o => o.q.EntryDate.ToString() == DateTime.Now.ToString("dd/MM/yyyy")).Where(o => o.q.Manager.ToString() == managerName).Where(o => o.q.THROUGH.ToString() != "CashTransfer")
                    .ToList();
Posted
Updated 21-Jun-16 1:36am
v2
Comments
Maciej Los 21-Jun-16 5:53am    
Date is date and nothing else! You shouldn't convert it into formated string!
Abhilask kumar 21-Jun-16 6:23am    
But in model and database, EntryDate is string type.

Please, read my comment to the question.

This should work as well:
C#
var data = db.expense
                    .Join(db.Itemmodels, q => q.ITEM, o => o.id, (q, ITEM) => new { q, ITEM })
                    .Where(o => o.q.EntryDate == DateTime.Today && o.q.Manager == managerName && o.q.THROUGH != "CashTransfer")
                    .ToList();
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 21-Jun-16 6:00am    
Hi Maciej
EntryDate == DateTime.Today
the above condition will never match at all. since it will check the Timestamp as well.
Maciej Los 21-Jun-16 6:16am    
It depends on data type of EntryDate field. If it's Date, not DateTime data type, the query should works as well. If not, you're right.
Thank you for your comment, Karthik.
Karthik_Mahalingam 21-Jun-16 6:25am    
even though it is Date Type (in db) it will be having value as "06/21/2016 00:00:00" in EntryDate( c# property) but the DateTime.Now will be having the Timestamp like "06/21/2016 15:53:21"
so it will never match at all.
and sorry if i am wrong.
Maciej Los 21-Jun-16 6:27am    
As you can see, i replaced DateTime.Now with DateTime.Today ;)
Karthik_Mahalingam 21-Jun-16 6:31am    
now it should be fine with the Date issue.
5 for it.
try like this
C#
string currentDate = DateTime.Now.ToString("dd/MM/yyyy");
      var data = db.expense.Join(db.Itemmodels, exp => exp.ITEMID, im => im.id, (exp, im) => new { exp = exp, im = im })
                     .Where(o => o.exp.EntryDate == currentDate &&  o.exp.Manager == managerName &&  o.exp.THROUGH != "CashTransfer")
                     .ToList();
 
Share this answer
 
v2
Comments
Maciej Los 21-Jun-16 8:08am    
Well... It meets OP requirements, although Where clause repeated 3 times looks ugly. ;(
Improve it, and you'll get 5 from me ;) Till now, a 4!
Karthik_Mahalingam 21-Jun-16 8:12am    
ha ha good catch.
done sir :)
Thank you.
Maciej Los 21-Jun-16 8:14am    
5ed!
Karthik_Mahalingam 21-Jun-16 8:18am    
Thank you Maciej Los
Abhilask kumar 22-Jun-16 0:54am    
Sir many many thank you.
and sir i'm new in mvc and entity framework.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900