[RESOLVED]How How to Filter into IList Object in MVC C#

Hello,

I have an query how to filter IList object by passing an ID as integer, Below is code 

 IList<F> ListResult  = objBll as IList<F>;

The above object contains 3 rows how to filter by passing ID = 1 to get the row.

Thanks,

Shabbir

You can use the where lambda expression or linq query

http://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx

Your code should be something similar

var result = ListResult.where(f => f.id == 1)

Basically i have below class which doesn’t exist in Database how to Cast as DbSet, So that i can use to my Basecontroller class.

public class Category_View
        {
            public int ID { get; set; }
            public int CalendarID { get; set; }
            public int CategoryID { get; set; }
            public string Title { get; set; }
            public string CategoryName { get; set; }
        }

Thanks in Advance

Shabbir

you cannot cast your class to dbset. either u use entity framework (this will map your class to your generated table/columns or it will generate your table automatically for u) or u have to write your business logic and data access logic yourself.

shabbir_215

shabbir_215

just implement conversion logic

public class
Category_View
       
{
           
public int ID
{ get;
set;
}
           
public int
CalendarID {
get;
set; }
           
public int
CategoryID {
get;
set; }
           
public string
Title {
get;
set; }
           
public string
CategoryName {
get;
set; }
       
}

Forexample:

private static Category_View ConvertToEntity(youDbSetType entity)

{

return new Category_View

{

Field1 = entity.Field1,

Field2= entity.Field2

};

}

Thanks Thai

I have add the below code but how to convert to DBset using below function

private static Category_View ConvertToEntity(Category_View entity)
        {

            return new Category_View
            {

                CalendarID = entity.CalendarID,
                CategoryID = entity.CategoryID,
                CategoryName = entity.CategoryName,
                ID= entity.ID,
                Title = entity.Title
            };

        }

I tried this way but didn’t workFrown

 protected DbSet<Category_View> dbEntitySetp = ConvertToEntity(Category_View) as DbSet<Category_View>;

projection operators

// fake dbSet returned

var c = new List<Category_View>()
            {
                new Category_View(),
                new Category_View(),
                new Category_View()
            };

IList<F> ListResult = new List<F>();

ListResult = (IList<F>) (from r in c select new {r.CalendarID}).Select(f => new F(f.CalendarID));}

Hi shabbir,

The DbSet<TEntity> is in the entity framework assembly, it bases on the DbContext. You can’t convert your class or normal collection to DbSet<TEntity>

# Using Repository Pattern in Entity Framework

http://blogs.msdn.com/b/wriju/archive/2013/08/23/using-repository-pattern-in-entity-framework.aspx

On the other hand, this issue is not related to original issue, please create a new thread for the new issue.

Best Regards

Starain

shabbir_215

shabbir_215

If I am understanding you. just convert it back to DbSet by implementing inversion converter. You have to implement to converters.

private static UDbSetTypeEntity
ConvertToEntity(DomainModel entity)
       
{

            return new
UDbSetTypeEntity

           
{

                CalendarID
= entity.CalendarID,
               
CategoryID
= entity.CategoryID,
               
CategoryName
= entity.CategoryName, ID= entity.ID,
               
Title = entity.Title
           
};

        }

It is extension methods so that how to use. It is associated with object.

var category = new Category();

var item = category.ConvertToEntity()

Leave a Reply