相关疑难解决方法(0)

返回匿名类型结果?

使用下面的简单示例,使用Linq to SQL从多个表返回结果的最佳方法是什么?

说我有两张桌子:

Dogs:   Name, Age, BreedId
Breeds: BreedId, BreedName
Run Code Online (Sandbox Code Playgroud)

我想用他们的狗归还所有的狗BreedName.我应该让所有的狗使用这样的东西没有问题:

public IQueryable<Dog> GetDogs()
{
    var db = new DogDataContext(ConnectString);
    var result = from d in db.Dogs
                 join b in db.Breeds on d.BreedId equals b.BreedId
                 select d;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

但如果我想要品种的狗并尝试这个我有问题:

public IQueryable<Dog> GetDogsWithBreedNames()
{
    var db = new DogDataContext(ConnectString);
    var result = from d in db.Dogs
                 join b in db.Breeds on d.BreedId equals b.BreedId
                 select new
                        {
                            Name = d.Name,
                            BreedName = b.BreedName
                        };
    return result; …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-sql

187
推荐指数
7
解决办法
19万
查看次数

从函数返回匿名类型

我可以在函数中使用匿名类型作为返回类型,然后将返回值的内容填充到某种数组或集合中,同时还向新数组/集合添加其他字段吗?请原谅我的伪代码......

private var GetRowGroups(string columnName)
{
var groupQuery = from table in _dataSetDataTable.AsEnumerable()
                             group table by new { column1 = table[columnName] }
                                 into groupedTable
                                 select new
                                 {
                                     groupName = groupedTable.Key.column1,
                                     rowSpan = groupedTable.Count()
                                 };
    return groupQuery;

}

private void CreateListofRowGroups()
{
    var RowGroupList = new List<????>();
    RowGroupList.Add(GetRowGroups("col1"));
    RowGroupList.Add(GetRowGroups("col2"));
    RowGroupList.Add(GetRowGroups("col3"));

}
Run Code Online (Sandbox Code Playgroud)

c# linq generics collections

21
推荐指数
2
解决办法
3万
查看次数

如何在linq-to-sql中返回一个匿名类型

我有以下伪代码,但想法是返回一个泛型类

 public static var getSeaOf(string user)
        {
            var periodsSettings = from p in sea
                                  where p.add_by == new Guid(user)
                                  select new { p.id, p.name };

            return var;
        }
Run Code Online (Sandbox Code Playgroud)

我在这里阅读 - 如何从使用LINQ to SQL的c#方法返回匿名类型,本案例的最佳解决方案是为返回类型创建一个类.

但我的问题是,如果我有数百个这样的函数,这是否意味着我需要有数百个类?

我希望有一个更通用的解决方案,谢谢你的帮助!!


我来看看

Silverlight - LinqToEntities - 如何返回匿名类型

但我不能在select new中指定类名,就像文章一样?

public static IEnumerable<retSea> getBskSeasonsOf(string user)
        {
            var periodsSettings = from p in sea
                                  where p.add_by == new Guid(user)
                                  select new retSea { p.id, p.name };

            return periodsSettings;
        }
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-sql c#-3.0 c#-4.0

6
推荐指数
1
解决办法
1560
查看次数

将函数作为参数传递给 Action

我试图通过一个方法作为一个动作,但似乎不是每说一次。

这就是我的做法:

public class RequestHandler<T> where T : struct
{
    public enum EmployeeRequests { BasicDetails, DependentsAndEmergencyContacts , MedicalHistory }

    protected Dictionary<T, Action> handlers = new Dictionary<T, Action>();

    protected EmployeeManagement empMgmnt = new EmployeeManagement();

    public void InitializeHandler(int employeeID)
    {

        this.AddHandler(EmployeeRequests.BasicDetails, () => empMgmnt.GetEmployeeBasicDetails(0));
    }

    public void AddHandler(T caseValue, Action action)
    {
        handlers.Add(caseValue, action);
    }

    public void RemoveHandler(T caseValue)
    {
        handlers.Remove(caseValue);
    }

    public void ExecuteHandler(T actualValue)
    {
        ExecuteHandler(actualValue, Enumerable.Empty<T>());
    }

    public void ExecuteHandler(T actualValue, IEnumerable<T> ensureExistence)
    {
        foreach(var val in ensureExistence)
            if …
Run Code Online (Sandbox Code Playgroud)

c# delegates action

4
推荐指数
1
解决办法
7555
查看次数

标签 统计

c# ×4

linq ×3

linq-to-sql ×2

action ×1

c#-3.0 ×1

c#-4.0 ×1

collections ×1

delegates ×1

generics ×1