[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I access this property created by LINQ?

Posted on 2009-04-17
22
Medium Priority
?
283 Views
Last Modified: 2013-11-11
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??
0
Comment
Question by:Camillia
  • 12
  • 10
22 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24172746
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
 
LVL 7

Author Comment

by:Camillia
ID: 24172783
#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
 
LVL 7

Author Comment

by:Camillia
ID: 24172800
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 39

Expert Comment

by:abel
ID: 24172802
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
 
LVL 7

Author Comment

by:Camillia
ID: 24172813
let me try this tonight. Thanks for sticking with this. Been at this for 2 days now!

I will post back.
0
 
LVL 39

Expert Comment

by:abel
ID: 24172820
> .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
 
LVL 7

Author Comment

by:Camillia
ID: 24172828
ah, let me try both methods. Thank you for responding. I will try and post back if i cant get this working.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24177937
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
 
LVL 39

Expert Comment

by:abel
ID: 24178578
> /***** 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
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24178585
> //***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
 
LVL 7

Author Comment

by:Camillia
ID: 24178814
let me read and try. Thanks for sticking with this.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24180430
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
 
LVL 39

Expert Comment

by:abel
ID: 24180563
> 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
 
LVL 39

Expert Comment

by:abel
ID: 24180566
>  //*** 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
 
LVL 7

Author Comment

by:Camillia
ID: 24180671
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
 
LVL 7

Author Comment

by:Camillia
ID: 24181188
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
 
LVL 39

Expert Comment

by:abel
ID: 24181235
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
 
LVL 7

Author Comment

by:Camillia
ID: 24181269
>> 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
 
LVL 7

Author Comment

by:Camillia
ID: 24181300
i think i have to do something like this. Let me try it

  List<ScoreCalculator> sc = new List<ScoreCalculator>();
           string test = setScores(sc);    
0
 
LVL 39

Expert Comment

by:abel
ID: 24181309
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
 
LVL 7

Author Comment

by:Camillia
ID: 24181516
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
 
LVL 39

Expert Comment

by:abel
ID: 24181523
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question