How do I access this property created by LINQ?

1.in my aspx page, i have this:
var result = from p in sCalculator.test(rule.CalculatorId)
select new OrderP.ScoreCalculator { typeName = p.TypeName, name = p.Name };

2. In my sCalculator, i have this:
public String typeName { get; set; }
public String name { get; set; }
public List<ScoreCalculator> test(Guid id)
{
var q =
from i in this.Context.ScoreCalculator
where i.Id == id
select i;

return q.ToList();
}

3.**** Now, In SRule.cs, (another file), i want to access "name" and "type" name. How do I do this??
LVL 8
CamilliaAsked:
Who is Participating?
 
abelCommented:
> //***I am in sRule.cs.  Call sCalculator.test(...) and get these this property. Can i do this?

yes, you can. You are given a list and you can go through that list and call the .test() on each and every one of them:

public void setScores(List<ScoreCalculator> newList){    foreach(ScoreCalculator calc in newList)    {        calc.test();    }}
0
 
abelCommented:
Is sRule part of the ASPX page? Do you mean to enumerate the result of test? In general, you can do this:

foreach (ScoreCalculator scoreCalc in sCalculator.test(rule.CalculatorId))
{
   string name = scoreCalc.name;
   string type = scoreCalc.typeName;
   // do something with each type and name...
}

but I'm not sure I understand your class structure, or your question, so please elaborate if I seem off-track.
0
 
CamilliaAuthor Commented:
#2 above: sCalcualtor.cs is a class with 2 properties. It also has a "test" routine that gets the values for the 2 properties.

#1 above: aspx calls this sCalculator.test rotuine. Gets the values and sets the class properties with LINQ: the section with "select".

#3. Now, sRule.cs is another class. I set the property values in the aspx page BUT I also need to access these 2 properties in sRule.cs. Maybe I need to set the properties of sCalculator in sRule and not in the aspx page??
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
CamilliaAuthor Commented:
so basically, how do i access "scoreCalc.name" in sRule while i'm setting this property in the aspx page? same with "scoreCalc.typeName;"...I'm setting these properties in the aspx page but i need to access the values in sRule.cs..
0
 
abelCommented:
You can access the class from any other class, you should just insert a

using NamespaceToYourClass;

directive on top of your sRule.cs class. Then you can access your sCalculater class any which way you like. You seem to both refer to sCalculator as the instance of the class and the class itself, but normally the name of the variable holding the instance of the class is different:

sCalculator myCalc = new sCalculator();
List<ScoreCalculator> listOfScores = myCalc.test(....);

0
 
CamilliaAuthor Commented:
let me try this tonight. Thanks for sticking with this. Been at this for 2 days now!

I will post back.
0
 
abelCommented:
> .I'm setting these properties in the aspx page but i need to access the values in sRule.cs..

well, now it gets tricky, because now we have to discuss the matter of scope. In ASPX, any objects are in general only "alive" during a request. If your ASPX page is loaded and it creates an object of the type sRule, then you can add a property to sRule and set it to the results. Suppose you have this in sRule:

public setScores(List<ScoreCalculator> newList){   // set a member field   m_Scores = newList;}
now you can set this in your ASPX code behind (assuming rule is an instance of sRule):

var result = from p in sCalculator.test(rule.CalculatorId)rule.setScores(result);select new OrderP.ScoreCalculator { typeName = p.TypeName, name = p.Name };
0
 
CamilliaAuthor Commented:
ah, let me try both methods. Thank you for responding. I will try and post back if i cant get this working.
0
 
CamilliaAuthor Commented:
ok, i have a problem.
1.in sRule.cs, I created this property:
         private List<ScoreCalculator> m_scores;
        public void setScores(List<ScoreCalculator> newList)
          {
              // set a member field
              m_scores = newList;

              foreach (var p in newList)
              {
                 
                  //p.typeName;
                  //p.name;
              }
         
          }

2. This doesnt work in the aspx page. I get an error on second line below;
     var res = from p in scoreCalculator.test(rule.CalculatorId)
                             select scoreRule.setScores(res);  //***** error: cannot use local variable res before it's declared.

*** So, i need to set the property and use them in sRule.cs. I dont care where I set these "setScores"'s typeName and name. As long as I can access typeName and name in sRule, i'll be fine. I dont have to set the property in the aspx page.

Can I do this in sRule:
  public void setScores(List<ScoreCalculator> newList)
          {
             //***I am in sRule.cs.  Call sCalculator.test(...) and get these this property. Can i do this?
          }
0
 
abelCommented:
> /***** error: cannot use local variable res before it's declared.

indeed. You should do something like this, before you use it:

var res = new something();

and then you can use it.

On a side note: you are using "var" all through your code, that makes it very tricky to debug, because none of your variables are typed. Meanwhile, you are trying to use a very complex technique: generics with lists, which is a way of making the (formerly untyped) lists typed.

Why trying to use typed variables on one end and untyped at the other? It is best to type all your variables and never use "var". If you do need an untyped variable, use "object" instead (I personally never use "var" at all...)

-- Abel --
0
 
CamilliaAuthor Commented:
let me read and try. Thanks for sticking with this.
0
 
CamilliaAuthor Commented:
I havent tried the second example yet but I was reading the error i get with "res". And you said..

>>var res = new something();

I shouldnt have to do that since I'm doing this: ( I already have "var res = from p..)
var res = from p in scoreCalculator.test(rule.CalculatorId)
                             select scoreRule.setScores(res);

I was trying to do what you posted below which didnt work.  (I will try the other method and post back)
now you can set this in your ASPX code behind (assuming rule is an instance of sRule):
 
 
var result = from p in sCalculator.test(rule.CalculatorId)
rule.setScores(result); //*** this doesnt work.I need a "select"
select new OrderP.ScoreCalculator { typeName = p.TypeName, name = p.Name };

Open in new window

0
 
abelCommented:
> I already have "var res = from p.

yes, but in the "from p..." you are also referencing res. So how is the compiler supposed to know what should be inside res before it is being given a value? In other words, how can the compiler possibly allow you to do setScores(res) when res does not exist yet?
0
 
abelCommented:
>  //*** this doesnt work.I need a "select"

that was only as an example, I assumed you would fill in the rest yourself.

I'm sorry, but you got me totally lost in what you are trying to accomplish. Can you show your whole class please? Including any referencing classes? It seems to me that you are mixing certain OO concepts with LINQ that cannot be mixed.
0
 
CamilliaAuthor Commented:
I think you're right.  I think i'm mixing OO concepts that might not go with LINQ. But let me try your example and will post back.
0
 
CamilliaAuthor Commented:
One last question. I think I'm close. I used this method in **** sRule.cs *** class. I have the actual code below. Now, how do I call this property to get _typeName back?? I run the code but because i'm not calling this property, I get NULL for _typeName.

 Can I call it in sRule.cs class or do I have to call it from my aspx page?
 in sRule class, i tried this
  _string test  = setScores(List<ScoreCalculator>); but this doesnt work...

----
public void setScores(List<ScoreCalculator> newList)
{
   foreach(ScoreCalculator calc in newList)
    {
       calc.test();
   }
}

  public string setScores(List<ScoreCalculator> calculatorType)
          {
            foreach (var ScoreCalculator in calculatorType)
              {
                  ScoreCalculator.test(_calculatorId);
 
                  _typeName = ScoreCalculator.typeName;
                 
              }
 
              return _typeName;
          }

Open in new window

0
 
abelCommented:
Remember this note from Microsoft on "var":

Overuse of var can make source code less readable  for others. It is recommended to use var only when it  is necessary, that is, when the variable will be used to store an anonymous type  or a collection of anonymous types.

In your code above, you have a list with types ScoreCalculator in it. Then you loop through that list (using a variable that you give the same name, please don't do that, it is very unreadable) where you assign to a certain field on each iteration.

By the end, the _typeName will have the typename of the last element in the list of calculatorType. Also, each object will have been called with the same _calculatorId.

Is that what you meant it should do?

If that was your idea, I suggest you rewrite your code like this, to make it slightly more readable:

public string setScores(List<ScoreCalculator> calculatorType)
{
    foreach (ScoreCalculator scoreCalc in calculatorType)
    {
         scoreCalc.test(_calculatorId);
         _typeName = scoreCalc.typeName;
    }
    return _typeName;
}

Open in new window

0
 
CamilliaAuthor Commented:
>> please don't do that, it is very unreadable
yes, i changed it. Thanks

>>Is that what you meant it should do?
 yes

But still, how do I call this to get/return the _typeName?? tried this but didnt work:
string test  = setScores(List<ScoreCalculator>);
0
 
CamilliaAuthor Commented:
i think i have to do something like this. Let me try it

  List<ScoreCalculator> sc = new List<ScoreCalculator>();
           string test = setScores(sc);    
0
 
abelCommented:
Neither will work. Your first statement is not complete. Your second statement start an empty list. The empty list will loop over an empty list (zero times, because there's nothing in) and will return an empty string (or whatever was in the field _typeName before).

Do something like this instead:


List<ScoreCalculator> sc = new List<ScoreCalculator>();sc.Add(new ScoreCalculator(yoursconstructor_arguments_here));sc.Add(new ScoreCalculator(yoursconstructor_arguments_here));sc.Add(new ScoreCalculator(yoursconstructor_arguments_here));string test = setScores(sc);

0
 
CamilliaAuthor Commented:
Finally, it works. Thanks so much for your help. I'm converting an old code to LINQ and confusing methods/concepts used. Thanks again.
0
 
abelCommented:
you're welcome. The LINQ syntax can be quite daunting to master. But I had the feeling this thread was more about generics and lists... All quite new concepts in the world of C#.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.