質問

This code is a sample part of my data layer ...

using (DataContext db = new DataContext(DBHelper.GetConnectionString()))
        {
          var result = from item in db.GetTable<Table1>()
                       select new { item};

          foreach (var obj in result)
                   {
                    Table1 retVal = new Table1();
                    retVal = obj.item;
                     List<Table2> tbl2List = new Table2Data().SelectTable2(retVal.Table2ID);
                    foreach (Table2 tbl in tbl2List)
                        {
                            retVal.Table2 += tbl;
                        }
                    retValList.Add(retVal);
                    }
}

So, I have a DataContext block, I get table in this way

db.GetTable<Table1>()

Because of my specific architecture ...

in the first loop of foreach, I fill main object with this code:

Table1 retVal = new Table1();
                    retVal = obj.item;

so retVal is my return value ...

For my Many-One relation , in main loop , i connect db each time and fetch data to fill object ...

This code:

List<Table2> tbl2List = new Table2Data().SelectTable2(retVal.Table2ID);
                    foreach (Table2 tbl in tbl2List)
                        {
                            retVal.Table2 += tbl;
                        }

I know this is not good but this is my way ..

And now my main question is:

How can I handle this Many-One relation ...

役に立ちましたか?

解決

Sadly , i prefer to use EF instead of Linq-To-SQL ...

var result = from item in db.Table1
                           .Include(a=> a.Table2)
                   select new { item};

If you have question to do that, feel free

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top