views:

88

answers:

2

I am building an asp.net site in .net framework 4.0, and I am stuck at the method that supposed to call a .cs class and get the query result back here is my method call and method

1: method call form aspx.cs page:

helper cls = new helper();
  var query = cls.GetQuery(GroupID,emailCap); 

2: Method in helper class:

public IQueryable<VariablesForIQueryble> GetQuery(int incomingGroupID, int incomingEmailCap)
    {
        var ctx = new some connection_Connection();
        ObjectSet<Members1> members = ctx.Members11;
        ObjectSet<groupMember> groupMembers = ctx.groupMembers;

        var query = from m in members
                    join gm in groupMembers on m.MemberID equals gm.MemID
                    where (gm.groupID == incomingGroupID) && (m.EmailCap == incomingEmailCap)
                    select new VariablesForIQueryble(m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap);
                    //select new {m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap};

        return query ;
    }

I tried the above code with IEnumerable too without any luck. This is the code for class VariablesForIQueryble:

3:Class it self for taking anonymouse type and cast it to proper types:

public class VariablesForIQueryble
{
    private int _emailCap;
    public int EmailCap
    {
        get { return _emailCap; }
        set { _emailCap = value; }
    }`....................................

4: and a constructor:

 public VariablesForIQueryble(int memberID, string memberFirst, string memberLast, string memberEmail, int? validEmail, int? emailCap)
    {

            this.EmailCap = (int) emailCap;
            .........................

    }

I can't seem to get the query result back, first it told me anonymous type problem, I made a class after reading this: link text; and now it tells me constructors with parameters not supported. Now I am an intermediate developer, is there an easy solution to this or do I have to take my query back to the .aspx.cs page.

A: 

If you want to project to a specific type .NET type like this you will need to force the query to actually happen using either .AsEnumerable() or .ToList() and then use .Select() against linq to objects.

You could leave your original anonymous type in to specify what you want back from the database, then call .ToList() on it and then .Select(...) to reproject.

You can also clean up your code somewhat by using an Entity Association between Groups and Members using a FK association in the database. Then the query becomes a much simpler:

var result = ctx.Members11.Include("Group").Where(m => m.Group.groupID == incomingGroupID && m.EmailCap == incomingEmailCap);

You still have the issue of having to do a select to specify which columns to return and then calling .ToList() to force execution before reprojecting to your new type.

Another alternative is to create a view in your database and import that as an Entity into the Entity Designer.

Hightechrider
Thanks rider, I went for reflections here is the link where I got the code and the idea from:http://stackoverflow.com/questions/874746/how-can-i-get-a-value-of-a-property-from-an-anonymous-typeI was thinking of making view but that would be too much because I have multiple test tables and regular tables where I had to get the data from, the idea of foreign key associations is good but again I had test tables. I tired ToList() method it gave me problems. Finally I had to go for reflection. I appreciate your help very much sir.
fzshah76
If you are going to use weak-typing like that you might as well use the new `dynamic` type in .NET 4 to get to the various fields on the result object. `.ToList()` should have worked fine: `(...query...).ToList().Select(x => new VariablesForIQuerable(...)).ToList();`
Hightechrider
A: 

Used reflection to solve the problem:

A: Query, not using custom made "VariablesForIQueryble" class any more:

 //Method in helper class 
 public IEnumerable GetQuery(int incomingGroupID, int incomingEmailCap)
        {

            var ctx = new some_Connection();
            ObjectSet<Members1> members = ctx.Members11;
            ObjectSet<groupMember> groupMembers = ctx.groupMembers;

            var query = from m in members
                        join gm in groupMembers on m.MemberID equals gm.MemID
                        where ((gm.groupID == incomingGroupID) && (m.EmailCap == incomingEmailCap)) //select m;
                        select new {  m.MemberID,  m.MemberFirst,  m.MemberLast, m.MemberEmail,  m.ValidEmail, m.EmailCap };
                        //select new VariablesForIQueryble (m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap);
            //List<object> lst = new List<object>();

            //foreach (var i in query)
            //{
            //    lst.Add(i.MemberEmail);
            //}
            //return lst;
            //return query.Select(x => new{x.MemberEmail,x.MemberID,x.ValidEmail,x.MemberFirst,x.MemberLast}).ToList();
            return query;
        }

B:Code to catch objects and conversion of those objects using reflection

  helper cls = new helper();
  var query = cls.GetQuery(GroupID,emailCap);      
 if (query != null)
                    {

                        foreach (var objRow in query)
                        {

                            System.Type type = objRow.GetType();
                            int memberId = (int)type.GetProperty("MemberID").GetValue(objRow, null);
                            string memberEmail = (string)type.GetProperty("MemberEmail").GetValue(objRow, null);
    }
    else
    {
    something else....
    }
fzshah76
I've tried this code too it's working but I think reflection are betterforeach(var objRow in query){string[] objects = objRow.ToString().Split(','); foreach (var o in objects.Where(x=>x.Contains("@"))) { if(o.Contains("@")) {do something}}}but there were 2 loops and more work.
fzshah76