如何从C#中的函数返回Generic.List
ASP.NET 3.5 C#
我正在使用Linq加入两张桌子。
表名是MapAssets和ExitPoint。
在数据库中他们与’有关系’有关
我正在我的BLL中编写一个函数来返回连接表
public List 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(); }
这显然不起作用。 我根本不知道该返回什么。
我对返回的所有约束都是能够绑定到gridview。
这是正确的方法吗? 或者最新的方式是什么?
你不能或者更好,唯一的方法是将它们装回盒子里的object
,但是这会使事情变得非常复杂,因为你不能将它们转换为任何类型(当然它是匿名的)而且你只能访问它们通过反思的属性….
在这种情况下,我强烈建议您创建一个自定义类。
编辑:
旁注…
如果您使用的是.net 4,那么事情会更容易,因为您可以返回dynamic
类型而不是object
(请查看此链接以查看dynamic
的简化),但我仍然希望创建自定义类。
看看如何从Method返回匿名类型。
https://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(object obj, T type) { return (T)obj; }
您可以使用下面提到的方法返回List和
List
希望这有助于不尝试。
您不能返回匿名类型,您只能在其所在方法的范围内使用匿名类型。您可能需要使用MapAssets / ExitPoints属性创建一个新类,并选择该类的新实例。
您正在尝试返回List ExitPoints和MapAssets列表,这是不可能的,因为您从两个表获取输出,即ExitPoints和MapAssets。 而且也无法返回匿名类型。 因此,为了重新启动查询,请创建一个类名ExMapClass,其中包含您需要作为查询输出的属性。 现在执行你编写的linq查询后迭代它即
创建新创建的类的列表
list newclass = new list();
foreach(var结果在ctx中){
实例化创建的类
obj.Property1 = var.MapAssets;
obj.Property2 = var.ExitPoints;
newclass.add(OBJ);
}
现在重新开始新创建的类列表。
希望你明白了。
你创建它之后是否必须绑定到这个对象? 如果没有,那么您可以创建一个“持久性AnonymousType”类,该类将值存储在字典中,并使用如下方法返回属性值:
string lastName AnonType.GetValue("LastName"); int age AnonType.GetValue("Age");
这是一个很好的例子的链接。 作者还有一个例子,他从数据表创建“AnonymousType”。
我已经开发了一个变体,我提供了使用以下语法查询 “AnonymousType”列表的function:
//这是查询var dept13 = anonAgents.AsQueryable()。Where(x => x.Has(“Department”,Compare.Equal,13);
//这是List的构造方式
private static AnonymousType ProvisionAgent(string name, int department) { return AnonymousType.Create(new { Name = name, Department = department }); } private List CreateAnonAgentList() { var anonAgents = new List (); // 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; }
只需使用和ArrayList
public static ArrayList GetMembersItems(string ProjectGuid) { ArrayList items = new ArrayList(); items.AddRange(yourVariable .Where(p => p.yourProperty == something) .ToList()); return items; }
这不行吗?
上述就是C#学习教程:如何从C#中的函数返回Generic.List分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
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();
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/988142.html