LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' - c#

So basically, I am trying to return a set of values to a GridView, which doesn't meet any of the values within an array. However, upon attempting I am getting the error of
LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' method, and this method cannot be translated into a store expression.
Here is my code:
public List<Room> getAvailRoom()
{
//Sessions from Default Page
DateTime checkedIn = Convert.ToDateTime(System.Web.HttpContext.Current.Session["checkIn"]);
DateTime checkedOut = Convert.ToDateTime(System.Web.HttpContext.Current.Session["checkOut"]);
//retrieves all the bookings which happen between two dates
var booking = (from b in context.Booking
where b.departureDate >= checkedIn && b.arrivalDate <= checkedOut
select b);
//Counts how many rooms are booked during those dates
int countRooms = booking.Count();
int[] bookings = new int[countRooms];
foreach (var booktypes in booking)
{
for (int i = 0; i < countRooms; i++)
{
//Addings the RoomIds to the array
bookings[i] = booktypes.RoomId;
}
}
//Returns values that does not equal to any roomIds within the bookings array.
return (from b in context.Room
where b.roomId != Convert.ToInt32(bookings.Any())
select b).ToList();
}
Any ideas what I am doing incorrectly?

You really were looking for a Contains query I think:
return (from b in context.Room
where !bookings.Contains(b.roomId)
select b).ToList();
Two problems with your snippet:
Any() returns a boolean - that's not at all what you want
You are trying to execute Convert.ToInt32 in SQL realm - there's no
equivalent to this there (basically Linq to Entities cannot convert this part to a SQL query), so this can't work.

Note: "bookings.Any()" will return a true or false indicating whether there is any element in the booking array. You are getting this error because you are trying to convert a boolean which is the result of the expression "bookings.Any()" to an integer.
If what you want to do is to get all rooms not booked then i will recommend
if(bookings.Any()){
return (from b in context.Room
where !bookings.contains(b.roomId)
select b).ToList();
}else{
return (from b in context.Room
select b).ToList();
}
meaning: if we have any room booked select those room that are not booked else select all the rooms(since no room is booked)
Note: Pls do your conversion outside link and use your result in your link statement if you need to do type conversion. Since this is C# i don't see you having any issue with type conversion here.
your "bookings[i] = booktypes.RoomId;" should return an array of int which require no conversion i.e "Convert.ToInt32" but if need be for this conversion do it before you i.e
if(bookings.Any()){
var thebooking = new List<int>();
foreach (var booking in bookings)
{
thebooking.Add(Convert.ToInt32(booking));
}
return (from b in context.Room
where !thebooking.contains(b.roomId)
select b).ToList();
}else{
return (from b in context.Room
select b).ToList();
}

Related

passing linq anonymous result to iqueryable object for another query

I have two tables (tbPerson and tbDataLog) where I need to return Id from one table (tbPerson) after checking certain conditions on both. After this, this result should be passed to another query. My first query returns the Id (primary key of a table) successfully and I need to pass these ids to another query so that it return me data based upon these Id. I also has an IQueryable type base object to check certain conditions to fetch data.
IQueryable<tbPerson> dataset
and I cannot changes this from Iqueryable to other as it will break other part of the code)
My first linq statement:
public static IQueryable<LogResults> GetResultsForYes()
{
Databasename ents = new Databasename();
var ids = (from f in ents.tbPerson
join g in ents.tbDataLog
on f.InfoID equals g.RefId
where g.Tag == "subscribed" && g.OldValue == "No" && g.Action == "Modified"
select new LogResults { _LogID = f.Id }).OrderBy(x => x._LogID);
return ids;
}
public class LogResults
{
public int _LogID { get; set; }
}
I access my result something like this where I can see in debugger all the Ids.
IQueryable<LogResults> log = GetResultsForYes();
Problem comes, when I tried to get records from tbPerson based upon these returned Id.
dataset=log.where(x=>x._LogID != 0);
I get this error:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists(are you missing a cast)?
Any suggestions or some other good approach is welcome.
I love this thing about stackoverflow. when we write questions we force our brain to think more deeply and after 30 mins of posting this question, I solved it in a simple way. Sometimes we overcomplicated things!
var ids = (from f in ents.tbPerson
join g in ents.tbDataLog
on f.InfoID equals g.RefId
where g.Tag == "subscribed" && g.OldValue == "No" && g.Action == "Modified"
select new { f.Id }).ToArray();
var allId = ids.Select(x => x.Id).ToArray();
dataset = dataset.Where(x => allId.Contains(x.Id));
#ankit_sharma : I have not tested yours but will give a try and come back to you. Thanks for giving time and effort.
IQueryable<tbPerson> dataset=log.where(x=>x._LogID != 0);
The result of log.where(x=>x._LogID != 0) is an IQueryable<LogResults>, and you are trying to assign this result to dataset of type IQueryable<tbPerson>, two diferent types.
EDIT:
I see you make a join to get the tbPerson ids, and then you do a second query to get the persons. You could get the persons in the first join.
I just modify your code:
IQueryable<tbPerson> persons = from person in ents.tbPerson
join g in ents.tbDataLog
on person.InfoID equals g.RefId
where g.Tag == "subscribed" && g.OldValue == "No" && g.Action == "Modified"
select person;

Linq Left Join with Where clause

I have this query that I am performing using entity framework 5 with MySql.
var employeeDetails = (from em in entities.employeemasters.AsEnumerable()
join sf in entities.salaryfitments.AsEnumerable()
on em.empID equals sf.empID into emsf
from x in emsf
where (x.edCode.ToString().Trim().Equals(txtEDCode.Text)
&& x.edCode != "SYS001")
select new { em, x });
The where (x.edCode.ToString().Trim().Equals(txtEDCode.Text) checks to see if there are any earnings/deductions stored for that employee and if so I can be able to get the amount figure.
I would like the query to return all employees and if they do not have a particular earnings/deductions matching txtEDCode.Text, then return a default value.
I cannot place .DefaultIfEmpty() after where (x.edCode.ToString().Trim().Equals(txtEDCode.Text)
What should I do to get the appropriate results?
Instead of returning the whole entities I'd create a new object with only the fields I was interested in and use a ternary if to provide the default value in the select statement, for example.
select new {
name = x.Name,
salary = x.Salary,
code = string.IsNullOrEmpty(x) ? "Blah" : x
}

How to get back result from a session

Work on entity frame work vs2010
After execute my linq query get a list of records ,want to put this record in session .Now from session Want to get back my record list ,what to do how to get back record from a session
Linq query
public IEnumerable GetSearchUserGroupPermissionData(int userID = 0)
{
var query = from p in this.Context.CmnPermissionGroupUsers
join q in this.Context.CmnPermissionGroups on p.PermissionGroupID equals q.PermissionGroupID
join r in this.Context.CmnPermissionGroupDocs on p.PermissionGroupID equals r.PermissionGroupID
join s in this.Context.CmnUserInfoes on p.UserID equals s.UserID
join t in this.Context.CmnDocLists on r.DocListID equals t.DocListID
//join u in this.Context.CmnModuleFormCompanies on t.ModuleID equals u.ModuleID
//join v in this.Context.CmnModuleLists on u.ModuleID equals v.ModuleID
//join w in this.Context.CmnFormLists on u.FormID equals w.FormID
where p.IsDeleted == false
select new
{
RecordID = p.PermissionGroupUserRecordID,
s.UserID,
s.UserFirstName,
q.PermissionGroupName,
p.EffectiveDate,
p.StatusID,
t.DocListID,
t.DocName,
t.ModuleID,
// v.ModuleName,
// u.FormID,
// t.FormName,
// w.FormName,
t.ParentID,
t.Sequence,
t.IsApprovalRequired,
t.CompanyCategoryID,
t.DocTypeID
//p.CreateBy,
//p.CreateOn,
//p.CreatePc,
//p.UpdateBy,
//p.UpdateOn,
//p.UpdatePc,
//p.IsDeleted,
//p.DeleteBy,
//p.DeleteOn,
//p.DeletePc,
//p.Transfer
};
return query.WhereIf(userID != 0, w => w.UserID == userID).ToList();
}
Put result in session
Session["UserPermission"] = new PermissionGroupUserController().GetSearchUserGroupPermissionData(objEntity.UserID);
Now ,want to get back the record set from session.bellow foreach syntax area as a item contain each row all properties and values but can not assign in a variable just like bellow ,why can not assign an AnonymousType variable value to a variable.
var o = Session["UserPermission"] as IEnumerable; //use casting
foreach (var area in o)
{
//int a = area.UserID;
}
Note:sabove syntax how me error
message:foreach statement cannot operate on variables of type 'object'
because 'object' does not contain a public definition for
'GetEnumerator'
If have any query please ask.
Did you try typecasting oto IEnumerable?
Apart from that, in your foreach loop, you have to use dynamic instead of var. This is required because your type is anonymous.
But i would still strongly suggest you to use normal types instead of Anonumous ones atleast for two reasons
Code reusability.
Better code readability.

Casting String as DateTime in LINQ

I have a table with a following format.
PID ID Label Value
------------------------------------------
1 1 First Name Jenna
1 2 DOB 10/12/1980
I need to retrieve all PIDs where First name starting with J and Month of DOB is 10.
in my code, I retrieve these in DataTable in C# and then tried to use LINQ to retrieve the results I want. This is just an example. These Labels could be anything user defines.
using LINQ I am able to retrieve all PIDs where First Name start with J, but every time I tried to Cast Value for DOB I get cast not valid error. I cannot change the column type in the database since Value could contain any type of information.
Here's a piece of my code. I am new to LINQ, and still trying to figure out around it.
var resultQuery = from r in query.AsEnumerable()
where (r.Field<string>("Label") == Label &&
r.Field<DateTime>("Value").Month == 10)
select r.Field<int>("PID");
Since not all items in the Value column of the table are convertible to DateTime, what you have will fail on invalid conversions. You can add in a clause that first checks that the value is a DateTime and only if it is, converts it and checks the .Month property.
DateTime d;
var resultQuery = from r in query.AsEnumerable()
where (r.Field<string>("Label") == Label &&
DateTime.TryParse(r.Field<string>("Value"), out d) &&
d.Month == 10)
select r.Field<int>("PID");
To potentially improve readability, you could also extract this out into a separate method:
var resultQuery = from r in query.AsEnumerable()
let d = TryGetDate(r.Field<string>("Value"))
where (r.Field<string>("Label") == Label &&
d != null &&
d.Month == 10)
select r.Field<int>("PID");
private DateTime? TryGetDate(string value)
{
DateTime d;
return DateTime.TryParse(value, out d) ? d : default(DateTime?);
}
You are going to end up filtering in memory which isn't very efficient.
So first select your data
var data= from r in query.AsEnumerable();
Then filter on the data
var filtered = from item in data
where item.Label == "Label"
&& Convert.ToDateTime(item.DOB).Month == 10
select item.PID;

c# group by doesn't work

i have some problems with my c# code everywhere in the Examples they do it like me but somehow i gonna get some errors
Compiler says at g.Datum he doesn' t know Datum
and at "return query" he says - cannot convert, there is a explicit convert
var query = (from p in dataContext.Untersuchungen
orderby p.Datum
group p by p.Datum into g
let number = (from n in dataContext.Untersuchungen
where n.Datum == g.Datum
select n).Count()
select new StatsistikObjekt() { Date1 = g.Datum, number1 = number });
return query;
hope you can help me =)
The type of the range variable g is the group, which indeed doesn't have a Datum value.
You can fix that bit easily, given your grouping (which uses Datum as the key)- and make your query simpler too by just counting the size of the group:
var query = (from p in dataContext.Untersuchungen
orderby p.Datum
group p by p.Datum into g
select new StatsistikObjekt() { Date1 = g.Key,
number1 = g.Count() });
As for the return value - we can't really help you on that one, as we don't know the return type you're trying to return.
Try
g.Key instead of g.Datum

Categories