LINQ - How to get the result of linq query into an array.

Asked By Sameer Khan on 06-Dec-11 12:35 AM
hi,
thanks friends for Chart reply,im done with my issues.
I have some issue on Linq
Below is my Linq Query
var test = from a in db.Results
        where a.ROLL == rollNo
        select new { a.sub1,a.sub2,a.sub3};
i want to take the result of this query either in an string array or in separate string variables.
I have tried this but giving me error.
string sub1= test.Select(tt => tt.sub1)--Error:cannot implicitly convert type int? to bool.
string sub2= test.Select(tt => tt.sub2)

and also 

string[] marks = test.Select(rr => rr.sub1);--Cannot implicitly convert type 'System.Linq.IQueryable<int?>' to 'string[]'
Please guide how can i do this. 
Jitendra Faye replied to Sameer Khan on 06-Dec-11 12:37 AM
Use this code

var query = from p in people
          where p.LastName.Length == 4
          select p.LastName;

    string[] names = query.ToArray();

    for (int i = 0; i < names.Length; i++)
      Console.WriteLine(names[i]);


Try this and let me know.
Riley K replied to Sameer Khan on 06-Dec-11 12:56 AM

No need to convert to ToArray if your resul is List then you can simply refer like using the index of list

List<Product> res = (from n in pCtx.OrdersDetails
        select new Product {ProductId=n.ProductID,CustName=n.CustomerName}).ToList();
 
    string cname = res[0].CustName;
     int pId = res[0].ProductId;

You have to define this class

public class Product
{
  public int ProductId { get; set; }
  public string CustName { get; set; }
 
 
}

Regards
Sameer Khan replied to Jitendra Faye on 06-Dec-11 12:58 AM
thanks for the reply.
but when i use 
 string[] names = query.ToArray();
i gets the same error again as cannot imlpicitly convert type 'myModel.Models.TableName[]' to 'string[]'
here TableName is  the name of my table.
Sameer Khan replied to Jitendra Faye on 06-Dec-11 01:16 AM
thanks frnds for ur replies.
im done with my issues.this is what im doing
var result="my earlier linq query"   
foreach (var item in result)
      {
         sub1= item.sub1.ToString();
         sub2= item.sub2.ToString();
      }
and now after getting these values im passing them to y-axis of my chart..
Frnds one more issue.
How can i get the sum() of subjects so that even if multiple records exists of that student then it should combine all marks of each subject and show the graph.
Asked By Sameer Khan on 06-Dec-11 01:29 AM
hi james 
i tried what u said.but the error remains 
Operator == cannot be applied to operands of type 'int?' and 'bool'
This time the red error line is marked under tt.sub1 == true 
Jitendra Faye replied to Sameer Khan on 06-Dec-11 01:38 AM
Change your code like this-

var


test = from a in db.Results

where a.ROLL == rollNo

select new { a.sub1, a.sub2, a.sub3 };


if(test .Count()>0))
{
string
sub1 = test.FirstOrDefault().sub1;

string sub2 = test.FirstOrDefault().sub2;

string sub3 = test.FirstOrDefault().sub3;

 }


Try this and let me know.
Jitendra Faye replied to Sameer Khan on 06-Dec-11 01:39 AM
If you want to get total then use this code-

var test = from a in db.Results

where a.ROLL == rollNo

select new { a.sub1, a.sub2, a.sub3 };


if(test .Count()>0))
{
string
sub1 = test.FirstOrDefault().sub1;

string sub2 = test.FirstOrDefault().sub2;

string sub3 = test.FirstOrDefault().sub3;

int total= Convert.ToInt32(sub1)+Convert.ToInt32(sub2)+Convert.ToInt32(sub3);

 }


TRy this and let me know.
Sameer Khan replied to Jitendra Faye on 06-Dec-11 01:57 AM
thanks vickey but i think i didnt expalined my issue more deeply.
I didnt want the sum of all subject marks in one variable.
If there are two record for rollNo=1 like this
RollNo     Sub1   Sub2
1                 50      40
1                 30      50
Earlier we are passing the records of each subject on the y-axis to the chart.
If a situation like above arises then what i want  is that the sum of each subject should be passed.As according to the data above.The total of Sub1 is 80 and sub2 is 90 then this 80 and 90 should be passed to the chart.
ie.sum of each subject individually.
Hope i explained my issue more clearer.