小编use*_*480的帖子

记录可以有一个可以为空的字段吗?

具有可以为空的字段的记录是合法的,例如:

type MyRec = { startDate : System.Nullable<DateTime>; }
Run Code Online (Sandbox Code Playgroud)

这个例子确实构建在我的项目中,但如果它是合法的,这是一个好的做法,如果有什么问题,这会引入什么?

f#

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

是否可以访问接口类型的其他成员?

我很好奇这是否可能,如果没有,那么它背后的原因是什么,以及如何处理这种编程方案?

假设我有这个界面:

public interface IBook
{
    void BookClient();
    void CancelClient();
}
Run Code Online (Sandbox Code Playgroud)

我有这个实现上述接口的类:

public class Photographer : IBook
{
     public void BookClient()
     {
        // do something
     }

     public void CancelClient()
     {
        // do something
     }

     // non-interface methods
     public void SaveClients()
     {
        // do something
     }

     public void DeleteClients()
     {
        // do something
     }
} 
Run Code Online (Sandbox Code Playgroud)

现在,如果我将此类分配给我的代码中的某个接口类型,例如:

IBook photo;
photo = new Photographer();
Run Code Online (Sandbox Code Playgroud)

是否有可能做到这一点:

// non-interface member from the Photographer class
photo.SaveClients();
Run Code Online (Sandbox Code Playgroud)

有人可以在这个问题上理顺我,也许可以指出我正确的方向.谢谢.

c#

2
推荐指数
1
解决办法
66
查看次数

我该怎么做才能改进这个F#代码?

我正在学习F#,所以我提出了一小段代码来帮助我掌握语言.我创建了下面的代码,从C#调用,然后保存上传的文件.我该如何改进这段代码?欢迎所有观点.

open System.Web
open System.IO

type SaveUpload(file: HttpPostedFileBase, path : string) = 

    // get the file name
    let origFileName = file.FileName

    // create the path including filename
    let filePath = Path.Combine(path, origFileName)

    // directory check
    let directory = Directory.Exists(path)

    // create directory
    let createDir x = 
        match x with
        |false -> Directory.CreateDirectory(path) |> ignore
        |true -> ()

    // save file
    let saveFile = 
        createDir directory
        if directory 
        then file.SaveAs(filePath)
Run Code Online (Sandbox Code Playgroud)

.net f#

2
推荐指数
1
解决办法
296
查看次数

使用查询表达式与Seq模块?

在操作通过FSharp.Data.SqlClient检索的数据时,在Sequence模块上使用查询表达式有什么优势吗?

例如:

query{
    for row in SelectAllCategories.Execute() do
    where row.Id = 1
    select {
        Id = row.Id;
        Category = row.CategoryName
    }
}
Run Code Online (Sandbox Code Playgroud)

SelectAllCategories.Execute()
|> Seq.filter (fun x -> x.Id = 1)
|> Seq.map (fun x ->
                {
                   Id = x.Id;
                   Category = x.CategoryName
                }
Run Code Online (Sandbox Code Playgroud)

就此而言,您甚至可以考虑使用LINQ.那么有什么好处,特别是关于FSharp.Data.SqlClient?

linq f# fsharp.data.sqlclient

2
推荐指数
1
解决办法
179
查看次数

为什么这两个相同类型的 F# 类对象不相等?

有人可以向我解释为什么这两个对象不相等,处理此类问题的解决方案是什么?

[<AllowNullLiteralAttribute>]
type Duration (id, startDate, endDate) =
    
    member val DbId : string = id with get,set
    member val StartDate : Nullable<DateTime> = startDate with get,set
    member val EndDate : Nullable<DateTime> = endDate with get,set

    new () =
        Duration(null, System.Nullable(), System.Nullable())

[<AllowNullLiteralAttribute>]
type ProductMedium (mediumType, fileLink, duration) =
    member val MediumType : string = mediumType with get,set
    member val FileLink : string = fileLink with get,set
    member val Duration : Duration = duration with get,set

    new () =
        ProductMedium (null, …
Run Code Online (Sandbox Code Playgroud)

.net f# .net-core

2
推荐指数
1
解决办法
51
查看次数

如何获取组中项目的索引

下面的代码给我一个参数超出范围的例外.我试图用ElementAt()exension方法获取当前索引的某些项目.我在这里想念的是什么:

var orders = cart.GroupBy(x => x.ClientForOrdersId).Select
            ((x, i) =>
             new Client_InvoiceBalance()
                 {
                     IsMainClient = x.Key == x.ElementAt(i).MainClientId ? true : false,
                     MainClientId = x.ElementAt(i).MainClientId,
                     OtherClientId = x.ElementAt(i).ClientForOrdersId,
                     InvoiceOrderNumber = orderNumber,
                     IsPaidInFull = false
                 }).ToList();
Run Code Online (Sandbox Code Playgroud)

c# linq

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

从实体框架通用存储库呈现IEnumerable <string>

我不确定这是否可行.我的数据库中有几个特定的​​表,由实体框架的代码第一类表示.这些类具有不同的属性,除了Id属性,它始终是一个字符串.我想知道是否还有创建一个通用的存储库,可以选择和呈现Id属性的序列.例如:

class GetDbIds<T> where T : class
{
    // PROPERTIES
    DbContext DbContext {get;set;}
    DbSet<T> DbSet {get;set;}

    // CONSTRUCTOR
    public GetDbIds(DbContext dbContext)
    {
         DbContext = dbContext;
         DbSet = DbContext.Set<T>();
    }

    // METHODS
    public IEnumerable<string> GenerateNewIdSequence()
    {
         return DbSet.Select(x => x.Id);
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道如何设置一个基本的通用存储库,但我没有遇到任何模式,也允许您动态查询存储库.

c# linq generics entity-framework

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

加入或条件

有没有更有效的方法来写这个?我不确定这是实现这一点的最佳方式。

select * 
from stat.UniqueTeams uTeam
Left Join stat.Matches match 
on match.AwayTeam = uTeam.Id or match.HomeTeam = uTeam.id
Run Code Online (Sandbox Code Playgroud)

t-sql sql-server

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

对象作为动作控制器的参数?

动作控制器是否可以接受文字对象.例如,我有几个视图,其中我想将各种模型发布到单个控制器,然后可以确定传入的模型对象以进行进一步处理.

型号样本:

public class Model1
{
   // properties, etc.
}

public class Model2
{
   // properties, etc.
}

public class Model3
{
   // properties, etc.
}
Run Code Online (Sandbox Code Playgroud)

控制器样本:

[HttpPost]
public ActionResult ProcessModel(Object anyModel)
{
   // determine the model
   if((anyModel as Model1) != null)
   {
     var model1 = anyModel as Model1;
     // continue with code
   }
   else if((anyModel as Model2) != null)
   {
     var model2 = anyModel as Model2;
     // continue with code
   }
   // continue with model check, etc.       
}
Run Code Online (Sandbox Code Playgroud)

我试过了,但我的控制器似乎没有拿起模型,因为我的对象参数仍然是空的.这可能吗?

c# asp.net-mvc

0
推荐指数
1
解决办法
2090
查看次数