shamim
shamim

Reputation: 6770

How to write extension method on ef

Work on EF 4 vs 2010.Face problem on extention method.Need help to write on extension method.

private void FillBillTaxOnSave(BillController objController, List<BilBillTAX> _entityTax, BilBillMaster objEntity)
        {
            foreach (BilBillTAX itemTax in _entityTax.FindAll(item => item.Action != Entity.ActionMode.Delete))
            {
                BilBillDetail tempDetail = objEntity.BilBillDetails.FirstOrDefault(item => item.BillDetailID == itemTax.BillDetailID);
                if (tempDetail != null)
                {
                    if (itemTax.Action == XERP.Entity.ActionMode.Add)
                    {
                        tempDetail.BilBillTAXes.Add(itemTax);
                    }                    
                }
            }
        }

Want to write extension method for the above syntax.

BilBillMaster objEntity = new BilBillMaster();
            List<TransactionItem> oList = new List<TransactionItem>();
            oList.SetSave(objEntity, item => item.Action != Entity.ActionMode.Delete, objEntity.BilBillDetails);

My extension method

public static IEnumerable<TSource> SetSave<TSource, T, TSourceDetail>(this IEnumerable<TSource> source, T entObj, Func<TSource, bool> predicate, IEnumerable<TSourceDetail> sourceDetail)
        {
            List<TSource> list = new List<TSource>();

            foreach (var element in source.Where(predicate))
            {

                if (element.Action == XERP.Entity.ActionMode.Add)
                {
                    sourceDetail.Add(element);
                }               
            }
            return list;
        }

My above syntax bellow portion show me error.

        if (element.Action == XERP.Entity.ActionMode.Add)
        {
            sourceDetail.Add(itemTax);
        }

If have any query please ask .thanks in advanced.Help me to compare values in extension method.

Error messages are:

2)  does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Collections.Generic.IEnumerable<TSourceDetail>' could be found

Upvotes: 0

Views: 507

Answers (1)

user1667253
user1667253

Reputation: 254

The Extension method that you wrote has no idea about the type TSource. In order for it to be able to understand you need to specify a constraints which restricts the type of TSource whatever you want.

In the example you had give you should add something like below.

public static IEnumerable<TSource> SetSave<TSource, T, TSourceDetail>(this IEnumerable<TSource> source, T entObj, Func<TSource, bool> predicate, IEnumerable<TSourceDetail> sourceDetail) where TSource : TransactionItem
{
    //Your logic goes here
}

Upvotes: 2

Related Questions