distinct

7 posts, 0 answers
  1. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 03 Oct 2009 Link to this post

    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?



  2. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 04 Oct 2009 Link to this post

    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 ?

  3. DevCraft banner
  4. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 04 Oct 2009 Link to this post

    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
  5. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 05 Oct 2009 Link to this post

    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

  6. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 05 Oct 2009 Link to this post

    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

  7. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 07 Oct 2009 Link to this post

    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 ?
  8. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 07 Oct 2009 Link to this post

    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.
Back to Top
DevCraft banner