0

in asp.net MVC Project, i have three tables FeeZone, FeeScheme and third table FeeSchemeZone to hold primary keys of first two tables ... as they are many=to=many relationship. now i have model classes for FeeZone, FeeScheme and in order to use LINQ and sametime to pass multiple models to single view i have created another class that wraps FeeZone and FeeScheme classes properties.

in step 1, i am getting error in LINQ query.... "Invalid Initializer member" in following section

Error Section

Obj1._FeeScheme.FeeSchemeID = a.FeeSchemeID,
Obj1._FeeZone.FeeZoneID = b.FeeZoneID,
Obj1._FeeZone.FeeZoneDescription = c.FeeZoneDescription

Model Wrap class

public class FeeSchemeZone
{

    public FeeZone _FeeZone;
    public FeeScheme _FeeScheme;
}

LINQ Query

 public IList<FeeSchemeZone> GetAllFeeZonesForFeeSchemeByID(int FeeSchemeID) 
    {
        FeeSchemeZone Obj1 = new FeeSchemeZone();

        using (var db = new QualificationContext())
        {
            var query = from a in db.FeeScheme
                        join b in db.FeeZoneSchema.Where(c => c.FeeSchemeID == 1) on a.FeeSchemeID equals b.FeeSchemeID
                        join c in db.FeeZone on b.FeeZoneID equals c.FeeZoneID
                        select new FeeSchemeZone
                        {
                            Obj1._FeeScheme.FeeSchemeID = a.FeeSchemeID,
                            Obj1._FeeZone.FeeZoneID = b.FeeZoneID,
                            Obj1._FeeZone.FeeZoneDescription = c.FeeZoneDescription
                        };

            return query.ToList();

        }
    }
K.Z
  • 5,201
  • 25
  • 104
  • 240
  • in the place where you get error, you are expected to set properties of newly created `FeeSchemeZone` there, but what you did is setting properties of `Obj1` instead. – har07 Jan 29 '14 at 09:02
  • aah! so i can i assign newly created data and assign in object so that i can use generated data in another class in foreach loop! – K.Z Jan 29 '14 at 09:05
  • I don't quite understand your point in the comment above. Anyway, I tried to post what could be the right [object initializer](http://msdn.microsoft.com/en-us/library/bb397680.aspx) syntax – har07 Jan 29 '14 at 09:17
  • After looking at your [previous question](http://stackoverflow.com/questions/21409777/return-linq-query-result-in-ilist-and-read-it-another-class) i think i get the point. And the answer seems to be yes, with this code (instead of what you tried previously) you can do foreach loop and access properties of each item in another class. – har07 Jan 29 '14 at 09:31
  • yes and i can use same class as ViewModel in order to pass multiple model to single view .... – K.Z Jan 29 '14 at 09:39

1 Answers1

1

As I said in the comment, in the place where you get error, you are expected to set properties of newly created FeeSchemeZone there, but what you did is setting properties of Obj1 instead. I'm not sure about the context, but the code should be something like this :

select new FeeSchemeZone
            {
                _FeeScheme = a,
                _FeeZone = c
            };

or this :

select new FeeSchemeZone
{
    _FeeScheme = new FeeScheme{ FeeSchemeID = a.FeeSchemeID },
    _FeeZone = new FeeZone { FeeZoneID = b.FeeZoneID, FeeZoneDescription = c.FeeZoneDescription }
};
har07
  • 88,338
  • 12
  • 84
  • 137
  • i tried but at run time... i am getting this error "An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code Additional information: The entity or complex type 'DatabaseLayer.DatabaseContext.FeeScheme' cannot be constructed in a LINQ to Entities query." – K.Z Jan 30 '14 at 05:40
  • which snippet you tried, is the first snippet caused the same error? – har07 Jan 30 '14 at 05:59
  • I just google-searched the error message, the second one seems not allowed in LINQ-to-Entities (will work for LINQ-to-objects). How about the 1st one? – har07 Jan 30 '14 at 06:14
  • no it is giving me error straight away; cannot implicitly convert .. to ... – K.Z Jan 30 '14 at 06:20
  • try with `_FeeZone = c` instead of `_FeeZone = b` – har07 Jan 30 '14 at 06:24
  • that is by design, we can't create new instance of mapped object in linq-to-entities select statement. In the 1st option we only create a non-mapped object (`FeeSchemeZone`). – har07 Jan 30 '14 at 07:09