In a dll (data access layer), I had for instance the functions (snip):

public System.Collections.IEnumerable getReportType()
        {
            var results = from a in db.user_defined_attribute
                          where ((a.adhoc == true) && (a.Active == 1))
                          orderby a.friendly_name
                          select new { Name = a.friendly_name, ID =
a.udf_id };
            return results.AsEnumerable();
        }

and the similar (identical)

public System.Collections.IEnumerable getTimePeriod()
        {
            var results = from a in db.auto_Col_Values
                          where ((a.col_id == 3) && (a.Active == true)
&& (a.attr_1 == "1"))
                          orderby a.name
                          select new { Name = a.name, ID =
a.custom_id };
            return results.AsEnumerable();
        }

It worked fine when called from the client app (databind).

I decided to add a "chose one" field, in the beginning.

I have tried this in the first function:

public System.Collections.IEnumerable getReportType()
        {
            var results = from a in db.user_defined_attribute
                          where ((a.adhoc == true) && (a.Active == 1))
                          orderby a.friendly_name
                          select new { Name = a.friendly_name, ID =
a.udf_id };
                          results = results.Union(new[] { new { Name =
chooseOne, ID = -1 } }).OrderBy(i => i.ID);
                          return results.AsEnumerable();
        }

It gave me an error like "Referencing Non-Scalar Variables Not
Supported" (see http://msdn.microsoft.com/en-us/library/bb896317.aspx).

I understand that it's because I was adding at the end. Odd, anyway.

Fine. I have changed the code to:

public System.Collections.IEnumerable getReportType()
        {
            var startingText = new[] {new { Name = chooseOne, ID =
-1 }};
            var results = from a in db.user_defined_attribute
                          where ((a.adhoc == true) && (a.Active == 1))
                          orderby a.friendly_name
                          select new { Name = a.friendly_name, ID =
a.udf_id };
            results =
startingText.Union(results).AsQueryable().OrderBy(i => i.ID);
            return results.AsEnumerable();
        }

        public System.Collections.IEnumerable getTimePeriod()
        {
            //var startingText = new[] { new { Name = chooseOne, ID =
-1 } };
            var results = from a in db.auto_Col_Values
                          where ((a.col_id == 3) && (a.Active == true)
&& (a.attr_1 == "1"))
                          orderby a.name
                          select new { Name = a.name, ID =
a.custom_id };
            //results =
startingText.Union(results).AsQueryable().OrderBy(i => i.ID);
            return results.AsEnumerable();
        }

so that I START with the fixed value instead of ending with one.

If I do this only in the first function, getReportType, it works fine.

As soon as I add the lines
   //var startingText = new[] { new { Name = chooseOne, ID = -1 } };
   //results = startingText.Union(results).AsQueryable().OrderBy(i =>
i.ID);
in the second function, I get a compilation error:

AnonymousType#1[] does not contain a definition for "Union".
It happens on "startingText.Union(results)" in the second function.
The first function is still alright. What ???

I have to confess, this is beyond my "expertise" in C#. What's
happening here ? How can you add a "chose one" item in these
collections (in the datasource, not in the client, in the aspx code,
via "add list item") ? What am I doing wrong here ?

Thank you very much
Alex.


PS. Note that a function that returns ONLY constants, like
public System.Collections.IEnumerable getReportPath()
        {
            return (new[] {
                new { Name = chooseOne, ID = 0 },
                new { Name = "AAAAAA", ID = 1 },
                new { Name = "BBBBBB", ID = 2 },
                new { Name = "CCCCCC", ID = 3 }
                });
        }

works just okay. The combobox binds just fine to this.

Reply via email to