This is a migrated thread and some comments may be shown as answers.

distinct

6 Answers 110 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
amir sherafatian
Top achievements
Rank 1
amir sherafatian asked on 03 Oct 2009, 02:09 PM
i have a persistent class (" Resource ") and in this table i have a field with name " Culture " i need to query that retrieve all culture in this table that doesn`t repetitive i use below sql code to do this before :

select distinct Culture from resources

in orm and with linq i use this function :

public IQueryable RetrieveCultures()
            {
                var query = (from q in scope.Extent<Resource>()
                             select new
                             {
                                 q.Culture
                             }).Distinct();

                return query;
            }


and then i call this function to bind return values to RadComboBox like this :

protected void RadComboBoxCultures_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBoxCultures.DataSource = resourceManager.RetrieveCultures();
        RadComboBoxCultures.DataBind();
    }

but i have this error :

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<EandE.Framework.Entities.Utilities.Resource>'. An explicit conversion exists (are you missing a cast?)

what is the right solution ? what is the right linq expression for distinct ? how can i do the right casting?



6 Answers, 1 is accepted

Sort by
0
amir sherafatian
Top achievements
Rank 1
answered on 04 Oct 2009, 07:51 AM

how can i get distinct ?

what is the right linq expression and how can i return my result set from function (what is the right cast to handle error that i tell above)?

is there any body to give me a sample function that return a result set that are dustinct ?

0
IT-Als
Top achievements
Rank 1
answered on 04 Oct 2009, 02:48 PM
Hellow amir,

Maybe I don't understand your class structure in full, so sorry if it is so...But it seems like you try to bind to a Resource instance, eventhough what is returned from the query is a Culture (don't know if this is a class or a simple type) instance

Makes sense?

Regards

HG
0
amir sherafatian
Top achievements
Rank 1
answered on 05 Oct 2009, 06:24 AM

i say my quesion in other side,
my Resource Class structure is like below :

[Telerik.OpenAccess.Persistent(IdentityField = "FId")]
    public class Resource
    {
        private int FId;
        private string FName;
        private string FDefaultValue;
        private string FValue;
        private string FCulture;
        private ModuleType FModuleType;
        private ObjectType FObjectType;
        private string FNote;
    }

i need a function that return all culture  as distinct,
my mean is ( select distinct Culture from Resource ) in TSql or ( (from q in db.Resource select new {q.Culture}).Distinct(); ) in Linq

and now my problem is :
a function with linq in open access that return distinct result set

0
IT-Als
Top achievements
Rank 1
answered on 05 Oct 2009, 06:58 AM
Hi amir,

Did you try the following:

public IQueryable RetrieveCultures()
            {
                var query = (from q in scope.Extent<Resource>()
                             select q.Culture).Distinct();
                return query;
            }

/HG

0
amir sherafatian
Top achievements
Rank 1
answered on 07 Oct 2009, 06:19 AM
hi again /
what can i do for this : ???
( select distinct Field1,Field2,.. from MyTable  )
is there any body here to give me a function like above with linq in OpenAccess that return resault set ?
0
IT-Als
Top achievements
Rank 1
answered on 07 Oct 2009, 07:15 AM
Hi amir,

I will try to explain further then. See the sample below:

        public List<object> GetUniqueCultures()
        {
            var q = (
                from r in scope.Extent<Resource>()
                select r.Culture)
                .Distinct();

            List<object> cultures = new List<object>();

            foreach (var c in q)
            {
                cultures.Add(new { Culture = c });
            }

            return cultures;
        }

The result of the above method you assign to your datasource like:

ComboBoxCultures.DataSource = GetUniqueCultures();

Does it make sense?

Regards

/HG


PS. You could also have a look in the "LinqDemonstrationProject" in the "Examples" folder within your OpenAccess installation directory, if you have not already done so.
Tags
LINQ (LINQ specific questions)
Asked by
amir sherafatian
Top achievements
Rank 1
Answers by
amir sherafatian
Top achievements
Rank 1
IT-Als
Top achievements
Rank 1
Share this question
or