Avatar of lulu50
lulu50
Flag for United States of America asked on

linq syntax error

Hi,

all I want to do is select FromDate value and store it in the FromDate parameter.

I can't fix my error.

the error is in this linq
 
 FromDate = (service.Certifications?.ToList()
                                   .Where(cert => IsServiceCertificationStatusValid(cert, umCase) && !cert.FromDate.IsNull && Enumerable.Range(1, 365).Contains(aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date.Subtract(cert.FromDate.Value).Days))
                                   .Select(cert => (DateTime)cert.FromDate?.Value)) ?? null;

Severity   Code   Description   Project   File   Line   Suppression State
Error   CS0266   Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.DateTime>' to 'BusinessRulesServer.BusinessObjectModel.NullableDateTime'. An explicit conversion exists (are you missing a cast?)   

I don't know how to fix that error. 
I just want to get the FromDate field from that linq. 


Open in new window


NullableDateTime FromDate = null;
        

                G.FnLogEvent($"{aQuestionnaireName}: Current Case Start Date {aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date}");

                aPayload.Member.Cases?.ToList().ForEach(umCase =>
                {
                    umCase.Services?.ToList().ForEach(service =>
                    {
                        if (service.ProcedureCode != null && G.FnIsCodeContainedInProcedureCodeGroup(service.ProcedureCode.Code, aQuestionnaireName))
                        {
                            FromDate = (service.Certifications?.ToList()
                                   .Where(cert => IsServiceCertificationStatusValid(cert, umCase) && !cert.FromDate.IsNull && Enumerable.Range(1, 365).Contains(aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date.Subtract(cert.FromDate.Value).Days))
                                   .Select(cert => (DateTime)cert.FromDate?.Value)) ?? null;
                        }
                    });
                });

Open in new window


Thanks,
Lulu


.NET MVC* ASP .NET MVC 5.NET ProgrammingC#LINQ Query

Avatar of undefined
Last Comment
lulu50

8/22/2022 - Mon
Andrei Fomitchev

When you use ToList you have a list:
List<carClass> cars = new List<carClass>();
cars = ["BMW", "Ford", "Toyota"].ToList()
foreach(carClass car in cars) {
     Console.WriteLine($"Car model: {car}");
}

 FromDate = (service.Certifications?.ToList() // It looks like you are trying to assign collection to a variable. Either FromDate should be a List<itemTypeOrClass> or you should iterate through the list and process dates item-by-item.

List<DateTime> dates = new List<DateTime>();
dates = ...ToList();
for each(DateTime item in dates) {
   FromDate = DateTime.Parse(item);
   // ... process FromDate
}
lulu50

ASKER
 /// </summary>
        /// <param name="ruleScenario"></param>
        /// <param name="reason"></param>
        private void PendCaseToMISTeamQueueVisitsDate(string ruleScenario, string reason, int visits)
        {


            string aQuestionnaireName = "Training";
                NullableDateTime FromDate = null;
        

                G.FnLogEvent($"{aQuestionnaireName}: Current Case Start Date {aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date}");

                aPayload.Member.Cases?.ToList().ForEach(umCase =>
                {
                    umCase.Services?.ToList().ForEach(service =>
                    {
                        if (service.ProcedureCode != null && G.FnIsCodeContainedInProcedureCodeGroup(service.ProcedureCode.Code, aQuestionnaireName))
                        {

//here I want to select 

Open in new window

FromDate from the linq below 
I don't know how 

Open in new window


                FromDate =    service.Certifications?.ToList()
                                   .Where(cert => IsServiceCertificationStatusValid(cert, umCase) && !cert.FromDate.IsNull && Enumerable.Range(1, 365).Contains(aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date.Subtract(cert.FromDate.Value).Days));
// can I add this this
.Select(cert => (DateTime)cert.FromDate?.Value)) ?? null;
                                  
                        }
                    });
                });


                G.UpgradeAssignment(aPayload, 5, ActionPriority.FYI, "Launch this action to assign the case to the next reviewer", false, 310, "MIS");
                G.setProviderReferenceAction(ActionPriority.RequiresAction, reason, G.DefaultReferenceActionURL, aPayload.Case);

                G.setReferenceAction(ActionPriority.RequiresAction, $"{ruleScenario}: Number of visits : {visits}", G.DefaultReferenceActionURL, aPayload.Case);
                G.FnLogEvent($"{ruleScenario}: Number of visits : {visits}");

                G.setReferenceAction(ActionPriority.RequiresAction, $"{ruleScenario}: FromDate  :  { FromDate.Value.toString() } ", G.DefaultReferenceActionURL, aPayload.Case);

                G.setReferenceAction(ActionPriority.RequiresAction, reason, G.DefaultReferenceActionURL, aPayload.Case);
            


        }

Open in new window

it_saige

You need to select just one FromDate, to do this you would use First or FirstOrDefault; e.g. -
FromDate = (service.Certifications?.ToList()
            .Where(cert => IsServiceCertificationStatusValid(cert, umCase) && !cert.FromDate.IsNull && Enumerable.Range(1, 365).Contains(aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date.Subtract(cert.FromDate.Value).Days))
            .FirstOrDefault(cert => (Nullable<DateTime>)cert.FromDate));

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
lulu50

ASKER
it_saige,
 
Oh I'm happy to hear from you!!!

you always know what I'm trying to say.

 ok back to the code:

FromDate = (service.Certifications?.ToList()
            .Where(cert => IsServiceCertificationStatusValid(cert, umCase) && !cert.FromDate.IsNull && Enumerable.Range(1, 365).Contains(aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date.Subtract(cert.FromDate.Value).Days))
            .FirstOrDefault(cert => (Nullable<DateTime>)cert.FromDate));

Open in new window


I'm getting this error:

Cannot Implicitly convert type 'System.DateTime?' to 'bool'

Open in new window

the error is on this:

 (Nullable<DateTime>)cert.FromDate)


ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lulu50

ASKER
it_saige,

It looks good but I want to change something.

I want instead of FromDate =
I want to change it to FromDate +=
so that way I have a list of all the dates.

but then I need to just get the last date from the list.
How can I do that ?

when I changed my code to += so I can add all the dates
I got this error:

Operator '+=' cannot be applied to operands of type 'NullableDateTime' and 'DateTime'


  aPayload.Member.Cases?.ToList().ForEach(umCase =>
                {
                    umCase.Services?.ToList().ForEach(service =>
                    {
                        if (service.ProcedureCode != null && G.FnIsCodeContainedInProcedureCodeGroup(service.ProcedureCode.Code, aQuestionnaireName))
                        {
                            FromDate += service.Certifications?.ToList()
                            .Where(cert => IsServiceCertificationStatusValid(cert, umCase) && !cert.FromDate.IsNull && Enumerable.Range(1, 365).Contains(aPayload.PayloadData.CurrentCaseStartDate.ToDateTime().Date.Subtract(cert.FromDate.Value).Days))
                            .Select(cert => (Nullable<DateTime>)cert.FromDate).FirstOrDefault();
                        }
                    });
                });

Open in new window


How can I get the last date from the FromDate list?
and
fix the Operator error? 

it_saige

So you can't use += on FromDate because it is a DateTime or Nullable<DateTime> type.  If you want to select multiple FromDate values then FromDate will need to be a List<Nullable<DateTime>>.  To get the last FromDate selected, you would use LastOrDefault instead of FirstOrDefault.

-saige-
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
lulu50

ASKER
oh ok
I got it
yes that make sense

I'm still testing



lulu50

ASKER
How can I display my FromDate value?
I'm getting a server error  on this line.
 G.FnLogEvent($"{ruleScenario}: FromDate : {(date)FromDate.Value}");
 
I just want to display the date without time. 
it_saige

G.FnLogEvent($"{ruleScenario}: FromDate : {FromDate.Value.ToString("MM\dd\yyyy")}");

Or whatever Date format you want.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

HTH,

-saige-
Your help has saved me hundreds of hours of internet surfing.
fblack61
lulu50

ASKER
it_saige,

Thank you so much for all your help.