2010-12-10 43 views
0

ASP.NET 3.5 C#
我使用Linq連接兩個表。
表名是MapAssets和ExitPoint。
在數據庫他們正在與相關的「有關係」如何從C中的函數返回Generic.List <Anonymoustype>#

我寫一個函數在我BLL返回連接表

 public List<ExitPoints> GetExitPointDetailsByProjectID(int iProjectID) 
     { 
      ctx = new CoreDBDataContext(); 
      var exitPointDetails = from ma in ctx.MapAssets 
            join ep in ctx.ExitPoints 
            on ma.MapAssetID equals ep.MapAssetID 
            where ma.ProjectID == iProjectID 
            select new 
            { 
             //would like to have data from both tables here 
             ctx.MapAssets, 
             ctx.ExitPoints 
            }; 
      return exitPointDetails.ToList(); 
     } 

這obviuosly不起作用。我不知道該怎麼回報。
我對返回的所有約束都是可以綁定到gridview的。
這是正確的方法嗎?否則最好的方法是什麼?

回答

6

你不能,或更好的,唯一的辦法是回到他們的object一個List盒裝,但是這個巨大的複雜的事情,因爲你不能將它們轉換成任何類型的(當然是匿名的),你只能通過反射訪問他們的屬性....

在這種情況下,我強烈建議你創建一個自定義類。

編輯:

在一個側面說明...
如果您使用.NET 4,事情會更容易些,因爲你可以回報dynamic Type代替object(看這個link看到dynamic「簡化),但我寧願創建一個自定義類。

+0

他用3.5,不能使用動態。 – Phill 2010-12-10 12:03:46

+0

在.net 3.5中沒有動態 – 2010-12-10 12:04:57

+0

哦,對不起,我沒有注意到版本。編輯謝謝;) – digEmAll 2010-12-10 12:12:14

1

您無法返回匿名類型,只能在其所在方法的範圍內使用匿名類型。您可能需要使用MapAssets/ExitPoints屬性創建一個新類並選擇該類的新實例。

-2

不會這項工作?

ctx = new CoreDBDataContext(); 
var exitPointDetails = from ma in ctx.MapAssets 
         join ep in ctx.ExitPoints 
          on ma.MapAssetID equals ep.MapAssetID 
         where ma.ProjectID == iProjectID 
         select Tuple.Create(ma, ep); 
return exitPointDetails.ToList(); 
+0

Tuple是.NET 4只。 – Euphoric 2010-12-10 12:24:08

+0

請注意,我很擔心返回類型 – naveen 2010-12-11 05:08:14

1

您試圖返回List ExitPoints和MapAssets列表,這是不可能的,因爲您從兩個表中獲取輸出,即ExitPoints和MapAssets。而且它也不可能返回一個匿名類型。因此,爲了重新查詢,請創建一個類名ExMapClass,其中包含您需要作爲查詢輸出的屬性。現在執行的哪個你寫迭代它即

創建新創建的類的列表

列表的NewClass =新名單LINQ查詢後();

的foreach(VAR結果在CTX) {

實例化創建的類

obj.Property1 = var.MapAssets;

obj.Property2 = var.ExitPoints;

newclass.add(obj);

}

現在重新列出新創建的類。

希望你明白了。

+0

感謝百萬隊友。我使用「選擇新的MyCustomClass」而不是「選擇新的」,而不是通過結果集重新迭代 – naveen 2010-12-11 05:10:55

1

創建它之後,您是否必須綁定到此對象?如果沒有,那麼你可以創建一個存儲在字典中的值,並用以下方法返回屬性值的「老大難AnonymousType」類:

string lastName AnonType.GetValue<string>("LastName"); 
int age AnonType.GetValue<int>("Age"); 

這裏是一個excellent example的鏈接。筆者也有一個example where he creates the "AnonymousType" from a datatable.

我已經在這個where I provide the ability to query「AnonymousType」列表下面的語法的變化工作:

//這裏的查詢 VAR dept13 = anonAgents.AsQueryable() 。其中(x => x.Has( 「部門」,Compare.Equal,13);

//這裏是清單是如何構建

private static AnonymousType ProvisionAgent(string name, int department) 
     { 
      return AnonymousType.Create(new 
      { 
       Name = name, 
       Department = department 
      }); 
     } 

     private List<AnonymousType> CreateAnonAgentList() 
     { 
      var anonAgents = new List<AnonymousType>(); 

      // Dave and Cal are in Department 13 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Dan Jacobs", 13, 44))); 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Calvin Jones", 13, 60))); 

      // Leasing = Dept 45 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Stanley Schmidt", 45, 36))); 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Jeff Piper", 45, 32))); 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Stewart Blum", 45, 41))); 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Stuart Green", 45, 38))); 

      // HR = Dept 21 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Brian Perth", 21, 25))); 
      anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Katherine McDonnel", 21, 23))); 

      return anonAgents; 
     } 
2

看一看如何返回匿名來自Method的類型。

http://forums.asp.net/t/1387455.aspx

複製鏈接中的代碼。

object ReturnAnonymous() 
{ 
    return new { Name="Faisal", City="Chakwal" }; 
} 

// Application entry-point 
void Main() 
{ 

    object o = ReturnAnonymous(); 

    // This call to 'Cast' method converts first parameter (object) to the 
    // same type as the type of second parameter - which is in this case 
    // anonymous type with 'Name' and 'City' properties 
    var typed = Cast(o, new { Name="", City="" }); 
    Console.WriteLine("Name={0}, City={1}", typed.Name, typed.City); 
} 

// Cast method - thanks to type inference when calling methods it 
// is possible to cast object to type without knowing the type name 
T Cast<T>(object obj, T type) 
{ 
    return (T)obj; 
} 

您可以使用這有助於沒試過下面提到返回列表的方法和

List<object> lstAnonymousTypes = GetExitPointDetailsByProjectID(1); 

foreach(object o in lstAnonymousTypes) 
{ 
    //Change it accordingly 
    var typed = Cast(o, new { new MapAssets() , new ExitPoints() }); 
} 

希望。

0

只是用和ArrayList

public static ArrayList GetMembersItems(string ProjectGuid) 
    { 
     ArrayList items = new ArrayList(); 
     items.AddRange(yourVariable 
         .Where(p => p.yourProperty == something) 
         .ToList()); 
     return items; 
    }