In my first line of code I am getting all available forms.
In the second line of code I'm getting all signed forms.
IEnumerable<ClinicForm> AllForms = db.ClinicForms.Where
(d => d.ClinicId == clinicId);
var SignedForms = db.SignedForms.Where
(d => d.FormSigned == d.ClinicForm.Id && d.PatientId==patientId);
Both of these lines of code work fine, but what I'm trying to do is get the list of all forms excluding the any of the signed forms.
I've made several attempts here.
var test = from c in AllForms
where !SignedForms.Contains(c.FormName)
select c;
This attempt results in an error:
Error 3 The best overloaded method match for 'System.Collections.Generic.List.Contains(SDatabaseLibrary.SignedForm)' has some invalid arguments
My second attempt:
var test2 = from y in AllForms
where !(from x in SignedForms
where x.FormSigned==x.ClinicForm.Id && x.PatientId==patientId
select x.ClinicForm).Contains(y.Id)
select y;
Error 5 Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Linq.IQueryable'
No doubt I am casting something wrong, but I'm not sure where.
Here is the final working solution, took a lot of playing with to get it working
var AllForms = db.ClinicForms.Where(d => d.ClinicId == clinicId);
var SignedForms = AllForms.Where(d => d.SignedForm.FormSigned == d.Id
&& d.SignedForm.PatientId == patientId);
var res = AllForms.Except(SignedForms);
Related
I wrote a query and worked on LINQPAD
from x in FacilityData
from y in FavInformation
where y.UserID == 1 && x.ID == y.FacilityID
select new
{
xID = x.ID,
xDistrictName = (from y in _Ilcelers
where y.ID == x.DistrictID
select y.IlceAd).FirstOrDefault(),
xName = x.Name,
Value = (from o in Tags
from p in Table_tags
where o.Prefix != null && o.Prefix == p._NAME && o.Facility == y.FacilityID
orderby p.İd descending
select new
{
FType = o.TagType,
Name = o.TagsName,
Value = p._VALUE,
Time = p._TIMESTAMP
}).Take(Tags.Count(h => h.Facility == y.FacilityID))
}
result
the result is perfect
but does not work in visual studio,
Value = (from o in DB.Tags
from p in DB.table_tags
where o.Prefix != null && o.Prefix == p.C_NAME && o.Facility == 11
orderby p.id descending
select new
{
FType=o.TagType,
Name = o.TagsName,
Value = p.C_VALUE,
Time = p.C_TIMESTAMP
}).Take(Tags.Count(h => h.Facility == y.FacilityID))
and it gives an error.
I guess the part with .Take() doesn't work because it's linq to EF.
error:
Limit must be a DbConstantExpression or a Db Parameter Reference Expression. Parametre name: count]
error image
thank you have a good day
Not sure but I will just throw it in. If you are talking about linq to ef/sql, it is possible they dont know a thing about C#. If take() would be the problem try to get the select result local first by doing .tolist(). Afterwards use your take funtion.
.ToList().Take(Tags.Count(h => h.Facility == y.FacilityID))
I am trying to write a LINQ statement using Entity Framework. I am getting an error stating "Expanding the Results View will enumerate the Enumerable"
My query is as follows :
IQueryable lis = (from que in _repo.Query<Question>()
where que.PresentationImageId == 1 join map in
_repo.Query<UserChildCourseQuestionMap>() on que.Id equals map.QuestionId into t
from rt in t.DefaultIfEmpty()
group t by que.Id
into g
select new
{
Id = g.Key,
QuestionBody = (from q in _repo.Query<Question>() where q.Id == g.Key select q.QuestionBody),
value = (from p in _repo.Query<UserChildCourseQuestionMap>()
where p.QuestionId == g.Key
select new
{
Name = gg.Key.AnswerOption,
Count = gg.Count(),
}).Union(from p in _repo.Query<UserChildCourseQuestionMap>()
where p.QuestionId == g.Key && p.UserInputText != null
group p by p.UserInputText into gg
select new
{
Name = gg.Key,
Count = gg.Count(),
}).Where(x => x.Name != null)
}
);
In LINQPad its working fine but in Visual Studio its not. Following is the image result which i am getting in LINQPad :
Please let me know where am I going wrong?
Following is the Screen Shot which i am getting when i expand the resultset:
Expand Image
The message " "Expanding the Results View will enumerate the Enumerable"" is not an error, it's a warning saying that if you expand the + sign the query will be run against the DDBB.
Just click in the + and expand the results tree, it should be ok.
I got the error
cannot implicity convert type 'System.Linq.IQueryable<EntityNetimoveis.San_Imovel>' to 'EntityNetimoveis.San_Imovel' An Explicit conversion exists (are you missing a cast ?)
This occurs when I try the following code
EntityNetimoveis.San2011Entities db = new EntityNetimoveis.San2011Entities();
EntityNetimoveis.San_Imovel im = db.San_Imovel.Where(a => a.Credenciada_Id == 10);
What type of conversion I have to do ?
EntityNetimoveis.San_Imovel im = db.San_Imovel.Where(a => a.Credenciada_Id == 10);
The type of im is an IQueryable<EntityNetimoveis.San_Imovel> because you are running a where query and there might be more than one result. If you want the first result matching your query you should use .FirstOrDefault(). Try changing you code as:
EntityNetimoveis.San_Imovel im = db.San_Imovel.FirstOrDefault(a => a.Credenciada_Id == 10);
If you want to return more than one entity and process them, lets say using a foreach loop, then you need to change the type of the return value to IEnumerable<Netimoveis.San_Imovel>. See the following example:
IEnumerable<EntityNetimoveis.San_Imovel> ims = db.San_Imovel.Where(a => a.Credenciada_Id == 10);
The you can use the following:
foreach(var im in ims) {
// your code goes here
}
If the lambda can return more than one record:
var imList = db.San_Imovel.Where(a => a.Credenciada_Id == 10).ToList();
I have code that calculate average number of days from date opened to date closed.
When i run the expression using Linq and lambda expression i get error as follows:
Error message: could not resolve property: DateClosed.DateCreated of:
TestProject.LegalWork
where code is:
var result = Repository.All
.Where(x => x.DateClosed != null)
.Average(x => ((DateTime)x.DateClosed - x.DateCreated).TotalDays);
How ever when i run this using loop and filter on condition only, eveything work fine.
int number= 0;
decimal totalDays = 0;
foreach (LegalWork legalWork in Repository.All.Where(x => x.DateClosed != null))
{
totalDays += (decimal)((DateTime)legalWork.DateClosed - legalWork.DateCreated).TotalDays;
number++;
}
return (totalDays == 0 || numberOfLegalWork ==0 ) ? 0 : Convert.ToDecimal(totalDays/numberOfLegalWork);
What is wrong in Linq and Lambda version?
I'm sure you can't call to:
x.DateClosed - x.DateCreated
or givenDate.TotalDays in linq2nhibernate, because you should call a specific function of DateTime which is not part of linq2nhibernate it's part of .net framework and it doesn't implemented in linq2nhibernate, but your current error message says another thing, to solve ur problem currently u can do:
var result = Repository.All.Where(x => x.DateClosed != null).ToList()
.Average(x => ((DateTime)x.DateClosed - x.DateCreated).TotalDays);
if there is problem with above code, please insert ur class definition,
but sure it's not a best solution, it's better to write a stored procedure and call it.
I've got the following situation:
var totalRecords = (from entry in invalidAccEntryRepository
select entry).Where(entry =>
entry.CustomerAccountInfo.CustomerAccountName == CustomerAccountName &&
entry.CustomerAccountInfo.CustomerAccountId == CustomerAccountId).Count();
vs.
var totalRecords = (from entry in invalidAccEntryRepository
select entry);
if (CustomerAccountInfoFilterActive)
{
totalRecords.Where(entry =>
entry.CustomerAccountInfo.CustomerAccountName == CustomerAccountName &&
entry.CustomerAccountInfo.CustomerAccountId == CustomerAccountId);
}
totalRecordsCount = totalRecords.Count();
The first query works completely but the second query only executes the only
var totalRecords = (from entry in invalidAccEntryRepository
select entry);
and ignores the conditionally added where expression.
Anyone know what the problem could be? Do you need more info?
Thx in advance!
You aren't actually applying the where. In your if, use this instead:
totalRecords = totalRecords.Where(entry =>
entry.CustomerAccountInfo.CustomerAccountName == CustomerAccountName &&
entry.CustomerAccountInfo.CustomerAccountId == CustomerAccountId);